UVa 10340 All in All

判断两字符串是否匹配

暴力出,注意数组要开大一点。

附AC代码:

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 
 6 char s[100010],t[100010];
 7 
 8 int main(){
 9     while(cin>>s>>t){
10         if(strstr(t,s)){//直接判断是否为字母串 
11         cout<<"Yes"<<endl;
12         continue;
13     }
14     int flag,sum=0;
15     int lens=strlen(s);
16     int lent=strlen(t);
17     for(int i=0;i<lens;i++){
18         flag=1;
19         for(int j=0;j<lent;j++){//匹配 
20             if(s[i]==t[j]){
21                 sum++;
22                 flag=0;
23                 strcpy(t,t+j+1);//去掉多余的字符 
24                 lent=strlen(t);
25                 //puts(t);
26                 break;
27             }
28             
29         }
30         if(flag){
31                 break;
32             }
33     }
34     if(flag){
35         cout<<"No"<<endl;
36         continue;
37     }
38     if(sum==strlen(s)){
39         cout<<"Yes"<<endl;
40     }
41     else{
42         cout<<"No"<<endl;
43     }
44     }
45     
46     
47     return 0;
48 }

还有一种更加简便的写法:

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 
 6 char s[100010],t[100010];
 7 
 8 int main(){
 9     while(cin>>s>>t){
10     int i,j;
11     for(i=0,j=0;s[i]!=''&&t[j]!='';j++)
12         if(s[i]==t[j])
13         i++;
14         if(s[i]=='')
15         cout<<"Yes"<<endl;
16         else
17         cout<<"No"<<endl;
18 }
19     return 0;
20 }

贴出来看看,反正在你们看来短就是好嘛~

原文地址:https://www.cnblogs.com/Kiven5197/p/5671862.html