求助#8~#10三个点TLE
查看原帖
求助#8~#10三个点TLE
200525
cks_楼主2023/8/15 11:18
#include <stdio.h>
#include <string.h>

void lower(char *s)
{
    for (int i = 0; i < (int)strlen(s); i++)
    {
        if (s[i] >= 'A' && s[i] <= 'Z')
        {
            s[i] = s[i] + 32;
        }
    }
}
int StrCount(char *text, char *word);
int StrCount(char *text, char *word)
{
    int n = 0;
    char *p;
    char *q;
    while (*text != '\0')
    {
        p = text;
        q = word;
        while ((*p == *q) && (*p != '\0') && (*q != '\0'))
        {
            p++;
            q++;
        }
        if (*q == '\0')
        {
            n++;
        }
        text++; // 下一个字符开始
    }
    return n;
}

int main()
{
    char in[11], str[1000001];

    gets(in);
    gets(str);
    lower(in);
    lower(str);
    char word[11] = {" "};
    char text[1000001] = {" "};
    strcat(word, in);
    strcat(word, " ");
    strcat(text, str);
    strcat(text, " ");

    char *p = strstr(text, word);
    if (p != NULL)
    {
        printf("%d", StrCount(text, word));
        printf(" %d", (int)(p - text));
    }
    else
    {
        printf("-1");
    }
}

写了好几个算法了,最后三个一直过不了,开了O2优化用时3.79s

2023/8/15 11:18
加载中...