同问
by zengwei @ 2019-05-06 10:48:32
```
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=1e7+5;
char sa[N],sb[N];
int p[N];
int main()
{
scanf("%s%s",sa+1,sb+1);//gets(sa+1);gets(sb+1);
int lena=strlen(sa+1);
int lenb=strlen(sb+1);
p[1]=0;int j=0;
for(int i=2;i<=lenb;++i)
{
j=p[i-1];
while(j>0&&sb[i]!=sb[j+1]) j=p[j];
if(sb[i]==sb[j+1]) p[i]=j+1;
else p[i]=0;
}
int st,ed;
j=0;
for(int i=1;i<=lena;++i)
{
while(j>0&&sa[i]!=sb[j+1]) j=p[j];
if(sa[i]==sb[j+1]) j++;
if(j==lenb)
{
st=i-lenb+1;
printf("%d\n",st);
}
}
for(int i=1;i<=lenb;++i) printf("%d ",p[i]);
}
```
by 寻旧 @ 2019-05-06 10:48:41
@[寻旧](/space/show?uid=49468) 不用 gets 就完了,gets 有毒
by xht @ 2019-05-06 10:51:22
@[xht37](/space/show?uid=100544)
好吧,蟹蟹
但我更想知道这毒在哪,
~~因为NOIP被坑过~~
by 寻旧 @ 2019-05-06 10:55:03
@[寻旧](/space/show?uid=49468) gets和string的getline一样,是读入一行的,所以有时会读不进去(~~getline和gets都有毒~~)
![跪.gif](https://i.loli.net/2019/05/06/5ccf96acc1e18.gif)
by Strong_Jelly @ 2019-05-06 11:02:05
~~我也被坑过~~
by Strong_Jelly @ 2019-05-06 11:02:43
@[神兵qqq1112](/space/show?uid=143681)
样例是怎么样的?
不就是读取一行吗?
~~字符太长读不进去?~~
by 寻旧 @ 2019-05-06 11:05:45
@[神兵qqq1112](/space/show?uid=143681)
同是天涯被坑人
qaq
by 寻旧 @ 2019-05-06 11:10:07
@[寻旧](/space/show?uid=49468) 我认为如果就读入一个字符串还好,读入多个字符串就会爆(~~亲身体验~~),如果没有要求读入空格,建议用scanf和cin
by Strong_Jelly @ 2019-05-06 11:11:29
@[神兵qqq1112](/space/show?uid=143681)
## 哇,谢谢啦~
by 寻旧 @ 2019-05-06 11:13:21