2017/8/14

来源:互联网 发布:新业汽修软件视频 编辑:程序博客网 时间:2024/05/21 07:14

今天就一题。。。还没过。。。

openjudge1.7.18从头到尾死循环到爆炸,简直了

(水一波)

描述

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

输入
输入两个字符串, 每个字符串占一行,长度不超过200且不含空格。
输出
若第一个串s1是第二个串s2的子串,则输出(s1) is substring of (s2) 
否则,若第二个串s2是第一个串s1的子串,输出(s2) is substring of (s1) 
否则,输出 No substring。

#include<bits/stdc++.h>using namespace std;int main(){ string s1,s2; cin >>s1; cin >>s2; int x=0,b=0,i=0; if (s1.length()>=s2.length()) { while ((x<s2.length())&&(i<s1.length())) { if (s2[x]==s1[i]) { b=1; x++; i++; } else { b=0; x=0;}}if (b==1)    cout <<s1 <<" is substring of " <<s2;else    cout <<"No substring";}else{ while((x<s1.length())&&(i<s2.length())) { if (s1[x]==s2[i]) { b=1; x++; i++; } else { b=0; x=0;}}if (b==1)    cout <<s2 <<" is substring of " <<s1;else    cout <<"No substring";}return 0;}
从上次放假开始还没过的题目。。。
真的这么难吗???

可怕




原来c++还有内置函数判断子串。。。

#include <iostream>#include <cstdio>#include <cstring>using namespace std;char a[101],b[101];int main(){    gets(a);    gets(b);    if(strstr(a,b)>0)        cout<<b<<" is substring of "<<a;    else     if(strstr(b,a)>0)            cout<<a<<" is substring of "<<b;        else            cout<<"No substring";}
就这么过了。。。


原创粉丝点击