wenbao与字符串处理

弱鸡代码

 1 #include <iostream>
 2 #include <string>
 3 #include <string.h>
 4 using namespace std;
 5 int main()
 6 {
 7     std::ios::sync_with_stdio(false);
 8     int n,i,j;
 9     string str1,str3,str2;
10     cin>>str1;
11     cin>>str2;
12     int len = str1.length();
13     for( i=0; i<len; i++)
14     {
15         if(str1[i]=='[')
16         {
17             n=0;
18             for( j=i+1;; j++)
19             {
20                 if(str1[j]==']')
21                 {
22                     for(int t=0; t<n-1; t++)
23                         str3+=str1[i-1];
24                     break;
25                 }
26                 else
27                 n=n*10+str1[j]-'0';
28             }
29             i=j;
30         }
31         else
32             str3+=str1[i];
33     }
34     string::size_type it;
35     it=str3.find(str2);
36     if(it!=string::npos)
37         cout<<"True"<<endl;
38     else
39         cout<<"False"<<endl;
40     return 0;
41 }

大神代码

  1 #include <cstdio>  
  2 #include <cstring>  
  3 #include <cmath>  
  4 #include <cstdlib>  
  5 #include<iostream>  
  6 using namespace std;  
  7 #define N 1005  
  8 typedef long long ll;  
  9 struct C{  
 10     int cnt;  
 11     char ch;  
 12 };  
 13 C A[N], B[N];  
 14 int main()  
 15 {  
 16     while (1)  
 17     {  
 18         for (int i = 0; i < N; i++)  
 19         {  
 20             A[i].cnt = 1;  
 21         }  
 22         char temp, t = ' ';  
 23         int cnt1 = -1, cnt2 = -1;  
 24         while (scanf("%c", &temp), temp != '
')  
 25         {  
 26             if (t == temp) A[cnt1].cnt++;  
 27             else if (temp != '[' && t != temp)  
 28             {  
 29                 cnt1++;  
 30             }  
 31   
 32             if (temp >= 'a' && temp <= 'z')  
 33             {  
 34                 A[cnt1].ch = temp;  
 35                 t = temp;  
 36             }  
 37             if (temp == '[')  
 38             {  
 39                 int num;  
 40                 scanf("%d%*c", &num);  
 41                 A[cnt1].cnt = num;  
 42             }  
 43         }  
 44         t = ' ';  
 45         while (scanf("%c", &temp), temp != '
')  
 46         {  
 47             if (temp >= 'a' && temp <= 'z' && temp != t)  
 48             {  
 49                 B[++cnt2].ch = temp;  
 50                 B[cnt2].cnt = 1;  
 51                 t = temp;  
 52             }  
 53             else if (temp == t)  
 54             {  
 55                 B[cnt2].cnt++;  
 56             }  
 57         }  
 58     /* 
 59     for(int i=0;i<=cnt1;i++) 
 60     cout<<A[i].ch<<" "; 
 61     cout<<endl; 
 62     for(int i=0;i<=cnt1;i++) 
 63         cout<<A[i].cnt<<" "; 
 64     cout<<endl; 
 65     for(int i=0;i<=cnt2;i++) 
 66         cout<<B[i].ch<<" "; 
 67     cout<<endl; 
 68     for(int i=0;i<=cnt2;i++) 
 69         cout<<B[i].cnt<<" "; 
 70     cout<<endl; 
 71     */  
 72         int flag = 0;  
 73         for (int i = 0; i <= cnt1; i++)  
 74         {  
 75             if (A[i].ch == B[0].ch && A[i].cnt >= B[0].cnt)  
 76             {  
 77                 int flag2 = 1;  
 78                 for (int j = 1; j < cnt2; j++)  
 79                 {  
 80                     if (A[i + j].ch != B[j].ch || A[i + j].cnt != B[j].cnt)  
 81                     {  
 82                         flag2 = 0;  
 83                         break;  
 84                     }  
 85                 }  
 86                 if (flag2)  
 87                 {  
 88                     if (A[i + cnt2].ch == B[cnt2].ch && A[i + cnt2].cnt >= B[cnt2].cnt)  
 89                     {  
 90                         flag = 1;  
 91                         break;  
 92                     }  
 93                 }  
 94             }  
 95         }  
 96         if (cnt1 == -1 && cnt2 == -1) break;  
 97         if (flag) printf("True
");  
 98         else printf("False
");  
 99     }  
100     return 0;  
101 }  

只有不断学习才能进步!

原文地址:https://www.cnblogs.com/wenbao/p/5737083.html