70分超时 求助!
查看原帖
70分超时 求助!
1496044
Scrame楼主2025/7/30 17:09

没有死循环的地方,但是还是超时了,de了好久也没明白

#include<stdio.h>
#include<string.h>
#include<ctype.h>

typedef long long ll;

char line[1000005], key[15], word[1000005];


int main()
{
    int cnt = 0, first = -1;
    scanf("%s", key);
    for(int i = 0; i < strlen(key); i++)
    {
        key[i] = tolower(key[i]);
    }
    getchar();
    fgets(line, 1000005, stdin);
    // line[strlen(line) - 1] = ' ';
    for(int i = 0; i < strlen(line); i++)
    {
        line[i] = tolower(line[i]);
    }
    int j = 0;
    for(int i = 0; i < strlen(line); i++)
    {
        if(line[i] != ' ')
        {
            word[j++] = line[i];
        }
        else
        {
            word[j] = '\0';
            j = 0;
            if(strcmp(word, key) == 0)
            {
                cnt++;
                if(first == -1)
                {
                    first = i - strlen(word);
                }
            }
            memset(word, 0, strlen(word));
        }
    }
    if(cnt)
    {
        printf("%d ", cnt);
    }
    printf("%d", first);
    return 0;
}

2025/7/30 17:09
加载中...