csp 201409-3 字符串匹配

问题描述:

  很简单,判断给定串是否在目标串中出现。分区分大小写和不区分大小写两种情况。


csp特点:

  csp的第三题一般是字符串的处理,c++的string类虽然不好用,但是掌握的话在考试的时候可能能省大量的时间。

 代码:

  

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<string>
 4 //利用c++的string类和algorithm的transform函数
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     int flag;
10     int n;
11     int fin;
12     string str,temp;
13     string s;
14     cin>>str;
15     cin>>flag>>n;
16     if(flag)
17     {
18         while(n--)
19         {
20             cin>>s;
21             if(s.find(str)!=string::npos)
22                 cout<<s<<endl;
23         }
24     }
25     else //关大小写敏感
26         {
27             transform(str.begin(),str.end(),str.begin(),::tolower);
28             while(n--)
29             {
30                 cin>>s;
31                 temp = s;//需要注意的是需要保存大小写转换前的结果
32                 //第一次的时候没有保存大小写转换前的s,只得一半分
33                 transform(s.begin(),s.end(),s.begin(),::tolower);
34                 if(s.find(str)!=string::npos)
35                     cout<<temp<<endl;
36             }
37         }
38     return 0;
39 }

猜你喜欢

转载自www.cnblogs.com/Crossea/p/11297197.html