给出两字符串,str1和str2,判断后字符串是否是前字符串的子串,如果是,输出YES,否则,输出NO。
示例输入
abc a 123456 45 abc ddd
示例输出
YES YES NO
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int next[1000010];
char a[1000010],b[1000010];
void getnext()
{
int i=0;
int j=-1;
next[0]=-1;
int len=strlen(b);
while(i<len)
{
if(j==-1||b[i]==b[j])
{
i++;
j++;
next[i]=j;
}
else
j=-1;
}
}
void get()
{
int i=0,j=0;
int len=strlen(a);
int len2=strlen(b);
while(i<len&&j<len2)
{
if(j==-1||a[i]==b[j])
{
i++;
j++;
}
else
j=-1;
}
if(j>=len2)
printf("YES\n");
else
printf("NO\n");
}
int main()
{
while(~scanf("%s",a))
{
scanf("%s",b);
getnext();
get();
}
return 0;
}