UVa 1588:Kickdown

来源:互联网 发布:互动投影软件1.1 编辑:程序博客网 时间:2024/04/30 15:52

题目传送门:UVa 1588:Kickdown

两个字符串,滑动匹配:

#include <stdio.h>#include <string.h>#define MAXLEN 105int 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;}
1 0
原创粉丝点击