九度oj题目1165:字符串匹配

题目1165:字符串匹配

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:3078

解决:1079

题目描述:

    读入数据string[ ],然后读入一个短字符串。要求查找string[ ]中和短字符串的所有匹配,输出行号、匹配字符串。匹配时不区分大小写,并且可以有一个用中括号表示的模式匹配。如“aa[123]bb”,就是说aa1bb、aa2bb、aa3bb都算匹配。

输入:

输入有多组数据。
每组数据第一行输入n(1<=n<=1000),从第二行开始输入n个字符串(不含空格),接下来输入一个匹配字符串。

输出:

输出匹配到的字符串的行号和该字符串(匹配时不区分大小写)。

样例输入:
4
Aab
a2B
ab
ABB
a[a2b]b
样例输出:
1 Aab
2 a2B
4 ABB
来源:
2008年北京航空航天大学计算机研究生机试真题
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <string>
 4 #include <queue>
 5 #include <stack>
 6 #include <string>
 7 #include <iostream>
 8 using namespace std;
 9 struct node{
10     string s1,s2;
11 };
12 node s[1005];
13 int main(){
14     //freopen("D:\INPUT.txt","r",stdin);
15     int n;
16     while(scanf("%d",&n)!=EOF){
17         int i=1,j;
18         for(;i<=n;i++){
19             cin>>s[i].s1;
20             s[i].s2=s[i].s1;
21             for(j=0;j<s[i].s2.length();j++){//全部转换为小写
22                 if(s[i].s2[j]>='A'&&s[i].s2[j]<='Z'){
23                     s[i].s2[j]=s[i].s2[j]-'A'+'a';
24                 }
25             }
26 
27             //cout<<i<<" "<<s[i].s2<<" "<<s[i].s1<<endl;
28 
29 
30         }
31         string ss;
32         cin>>ss;
33 
34         //cout<<ss<<endl;
35 
36         for(j=0;j<ss.length();j++){
37             if(ss[j]>='A'&&ss[j]<='Z'){
38                     ss[j]=ss[j]-'A'+'a';
39                 }
40         }
41 
42         //cout<<ss<<endl;
43 
44         for(i=0;i<ss.length();i++){
45             if(ss[i]=='['){
46                 break;
47             }
48         }
49 
50         //cout<<i<<endl;
51 
52         if(i==ss.length()){//没有中括号
53             for(i=1;i<=n;i++){
54                 if(ss==s[i].s2){
55                     cout<<i<<" "<<s[i].s1<<endl;
56                 }
57             }
58         }
59         else{
60             string s1,s2,s3,s4;
61             s1=ss.substr(0,i);
62 
63             //cout<<s1<<endl;
64 
65             i++;
66             int k=i;
67             for(;ss[i]!=']';i++);
68             s2=ss.substr(k,i-k);
69 
70             //cout<<s2<<" "<<s2.length()<<endl;
71 
72             s3=ss.substr(i+1);
73 
74             //cout<<s3<<endl;
75 
76             for(i=0;i<s2.length();i++){
77                 s4=s1+s2[i]+s3;
78 
79                 //cout<<s4<<endl;
80 
81                 for(j=1;j<=n;j++){
82 
83                      //cout<<i<<"  "<<s[i].s2<<endl;
84 
85                      if(s4==s[j].s2){
86                            cout<<j<<" "<<s[j].s1<<endl;
87                      }
88                 }
89             }
90         }
91     }
92     return 0;
93 }
原文地址:https://www.cnblogs.com/Deribs4/p/4650549.html