判断子串

s1是s2的子串
 

#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
");
    else
        printf("NO
");
    return 0;
}

你有n个字符串。 每个字符串由小写英文字母组成。 重新排序给定的字符串,使得对于每个字符串,在它之前的所有字符串都是它的子串。

如果可以在b中选择几个连续的字母以形成a, 那么a是b的子串。 例如,字符串“for”是“codeforces”,“for”和“therefore”的子串,但不是“four”,“fofo”和“rof”的子串。

Input

第一行包含整数n(1 ≤ n ≤ 100) - 字符串的数量。

接下来的n行包含给定的字符串。 每个字符串中的字母数不超过100。 每个字符串由小写英文字母组成。

可能会有相同的字符串。

Output

如果无法按照需要的顺序重新排列n个给定的字符串,请输出“NO”(不含引号)。

否则打印“YES”(不带引号)和排序号的n个的字符串。

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
");
        for(i=0;i<m;i++)
            puts(ch[i].s);
    }
    else
        printf("NO
");
    return 0;
}

还有一种方法可以判断字串   用string类型的find函数

有一个例题    https://blog.csdn.net/ZCY19990813/article/details/81394713

原文地址:https://www.cnblogs.com/zcy19990813/p/9702774.html