UVa 1588:Kickdown

题目传送门:UVa 1588:Kickdown

两个字符串,滑动匹配:

#include <stdio.h>
#include <string.h>
#define MAXLEN 105

int getMaxLength(char master[], int lenM, char driven[], int lenD)
{
    int i = 0, j = 0, matchPos = 0; // matchPos保存匹配的第一个位置
    while (i < lenM && j < lenD && matchPos < lenM)
    {
        if (master[i] - '0' + driven[j] - '0' <= 3)
        {
            ++i; ++j;
        }
        else
        {
            ++matchPos;
            i = matchPos;
            j = 0;
        }
    }
    if (lenM - matchPos >= lenD)
        return lenM;
    return lenD + matchPos;
}

int main()
{
    char master[MAXLEN], driven[MAXLEN];
    while (EOF != scanf("%s%s", master, driven))
    {
        int lenM = strlen(master);
        int lenD = strlen(driven);
        int x = getMaxLength(master, lenM, driven, lenD);
        int y = getMaxLength(driven, lenD, master, lenM);
        printf("%d\n", x < y ? x : y);
    }
    return 0;
}
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页