验证子串

验证子串


链接:http://ybt.ssoier.cn:8088/problem_show.php?pid=1140

【题目描述】

输入两个字符串,验证其中一个串是否为另一个串的子串。

【输入】

输入两个字符串, 每个字符串占一行,长度不超过200且不含空格。

【输出】

若第一个串s1是第二个串s2的子串,则输出(s1) is substring of (s2)

否则,若第二个串s2是第一个串s1的子串,输出(s2) is substring of (s1)

否则,输出 No substring。

 

【输入样例】

abc
dddncabca

【输出样例】

abc is substring of dddncabca
#include<iostream>

using namespace std;

void cmp(string a,int la,string b,int lb){
    int flag=0;
    for(int i=0;i<la;i++){
        int j=0,m=i;
        while(a[m++]==b[j++]&&a[m-1]!='\0')continue;
        if(j==lb+1){
            cout<<b<<" is substring of "<<a<<endl;flag=1;break;
        }
    }
    if(!flag)cout<<"No substring"<<endl;
}

int main(){
    string s1,s2;
    cin>>s1>>s2;
    int l1=s1.size(),l2=s2.size();
    if(l1>=l2) cmp(s1,l1,s2,l2);
    else if(l1<l2) cmp(s2,l2,s1,l1);
} 

 

posted @ 2017-08-09 18:25  Ed_Sheeran  阅读(1313)  评论(0编辑  收藏  举报