求大佬解疑,为什么我的输入改为cin就编译失败,改为scanf就可以通过
查看原帖
求大佬解疑,为什么我的输入改为cin就编译失败,改为scanf就可以通过
841612
xian_love楼主2024/9/23 18:11
这是没过的代码,一直显示编译失败
#include<bits/stdc++.h>
using namespace std;
const int N = 1000010;
char s1[N],s2[N]; // s1是目标字符串,s2是子串
int ne[N];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> s1 + 1 >> s2 + 1;
    int n = strlen(s2 + 1);
    int m = strlen(s1 + 1);
    //求解ne数组
    for (int i = 2,j = 0;i <= n;i ++)
    {
        while (j && s2[i] != s2[j + 1])j = ne[j];
        if(s2[i] == s2[j + 1])j ++;
        ne[i] = j;
    }

    //kmp匹配过程
    for(int i = 1,j = 0;i <= m;i ++)
    {
        while (j && s2[j + 1] != s1[i])j = ne[j];
        if (s2[j + 1] == s1[i])j ++;
        //匹配成功
        if (j == n)
        {
            printf("%d\n",i - n + 1);
            j = ne[j];
        }
    }
    for (int i = 1;i <= n;i ++)printf("%d ",ne[i]);
    return 0;
}

然后改为scanf就过了

#include<bits/stdc++.h>
using namespace std;
const int N = 1000010;
char s1[N],s2[N]; // s1是目标字符串,s2是子串
int ne[N];
int main()
{
    scanf("%s%s",s1 + 1,s2 + 1);
    // cin >> s1 + 1 >> s2 + 1;
    int n = strlen(s2 + 1);
    int m = strlen(s1 + 1);
    //求解ne数组
    for (int i = 2,j = 0;i <= n;i ++)
    {
        while (j && s2[i] != s2[j + 1])j = ne[j];
        if(s2[i] == s2[j + 1])j ++;
        ne[i] = j;
    }

    //kmp匹配过程
    for(int i = 1,j = 0;i <= m;i ++)
    {
        while (j && s2[j + 1] != s1[i])j = ne[j];
        if (s2[j + 1] == s1[i])j ++;
        //匹配成功
        if (j == n)
        {
            printf("%d\n",i - n + 1);
            j = ne[j];
        }
    }
    for (int i = 1;i <= n;i ++)printf("%d ",ne[i]);
    return 0;
}
2024/9/23 18:11
加载中...