90分,求助!#9 WA 了(一个很长的输入)
查看原帖
90分,求助!#9 WA 了(一个很长的输入)
1067945
SymmFz楼主2023/10/8 01:04

#9 输入的前面一部分: 第一行i 第二行R QjDwnEr akK KmV S PI Gv sXtO earvSqL iY i P NTD qf

。。。。。后面很长 输出: 2794 45

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int position = -1, cnt = 0, retPos = 0; 
  // position:总输入位置, cnt:匹配的单词数, 首个匹配单词的首字母位置(都是从0开始)
    char inWord[11] = {0}; //匹配单词
    char tempWord[25] = {0}; // >10 no
    int inWordEndIndex = 0; // inWord index of '\n'
    
    for (int i=0; i<=10; i++)
    {
        char ch;
        scanf("%c", &ch);
        if (ch >= 'A' && ch <= 'Z') { ch = ch + 'a' - 'A'; }
        inWord[i] = ch;
        if (inWord[i] == '\n') {
            inWordEndIndex = i;
            break;
        }
    }

    int state = 0, everyRight = 1; // no space
    for (int i=0; i<=1000005; i++)
    {
        // whole word input
        int j;
        everyRight = 1;
        for (j=0; j<=20; j++)
        {
           // state = 0;
            char ch;
            scanf("%c", &ch);
            if (ch >= 'A' && ch <= 'Z') {
                ch = ch + 'a' - 'A';
            }
            tempWord[j] = ch;

            position++;

            if (tempWord[j] == ' ')
            {
                state = 0;
                break;
            }
            if (tempWord[j] == '\n' || tempWord[j] == EOF) {
                state = 1; // 跳出最外层循环的标志位
                break;
            }
            if (tempWord[j] != inWord[j]) {
                everyRight = 0; // 单词匹配
            }
        }

        if (everyRight == 1 && inWordEndIndex == j) // 避免 abcdef 也能 和 abc匹配(如果abc是第一行输入)
        {
            cnt++;
            if (cnt == 1) {
                retPos = position - j; // first time position
            }
        }
        if (state == 1) {
            break;
        }
    }

    if (cnt == 0) {
        printf("-1");
    } 
    else
    printf("%d %d", cnt, retPos);
   // system("PAUSE");
}
2023/10/8 01:04
加载中...