#include <stdio.h>
#include <string.h>
int main()
{
char s1[100],s2[100],t[100];
int i,j,n1,n2;
gets(s1);
gets(s2);
n1=strlen(s1);
n2=strlen(s2);
for(i=0;i<n2-n1+1;i++)
{
memset(t,0,sizeof(t));
int e=0;
for(j=i;j<i+n1;j++)
t[e++]=s2[j];
if(strcmp(t,s1)==0)
break;
}
if(i<n2-n1+1)
printf("YES\n");
else
printf("NO\n");
return 0;
}
Input
Output
Sample Input
Input
5 a aba abacaba ba aba
Output
YES a ba aba aba abacaba
Input
5 a abacaba ba aba abab
Output
NO
Input
3 qwerty qwerty qwerty
Output
YES qwerty qwerty qwerty
Sample Output
Hint
在第二个示例中,您不能对字符串重新排序,因为字符串“abab”不是字符串“abacaba”的子字符串。
#include<stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
struct CHUAN
{
char s[101];
int n;
}ch[101];
int cmp(struct CHUAN a,struct CHUAN b)
{
return a.n<b.n;
}
int sub(int i)
{
int e=0,j,k;
char t[101];
for(j=0;j<ch[i].n-ch[i-1].n+1;j++)
{
memset(t,0,sizeof(t));
e=0;
for(k=j;k<ch[i-1].n+j;k++)
t[e++]=ch[i].s[k];
if(strcmp(t,ch[i-1].s)==0)
break;
}
if(j<ch[i].n-ch[i-1].n+1)
return 1;
else
return 0;
}
int main()
{
int m,i,sum=0;
scanf("%d",&m);
getchar();
for(i=0;i<m;i++)
{
gets(ch[i].s);
ch[i].n=strlen(ch[i].s);
}
sort(ch,ch+m,cmp);
for(i=1;i<m;i++)
{
int p=sub(i);
if(p==0)
sum++;
}
if(sum==0)
{
printf("YES\n");
for(i=0;i<m;i++)
puts(ch[i].s);
}
else
printf("NO\n");
return 0;
}