题目内容:

老师给小学生门布置了一些作业,让它们按照一个模版写一些字符串交上来,

同学们把作业交上来了,问题来了,这么多的作业老师批改不过来,现在请你帮老师

写一个程序,帮助老师确定各个字符串是否合格。

    首先老师有一个匹配模版,比如是“aa[123]bb”这一个字符串,同学们交的各种

作业字符串如aa1bb、aa2bb、aa3bb都算是正确匹配看,而aacbb就是错误的字符串。

(即待查字符串对应于模版方括号内的部分,应该为方括号内字符串的一个子字符)。

    我们需要做的就是按照模版,找出正确的字符串和所在的行。

 

输入格式:

输入的第一行为一个整数n,表示有多少个学生的作业,即有多少行需要检查的字符串。(1<=n<=50)

中间为n行字符串,代表着n个学生们写的作业。每个字符串长度小于50。

最后一行为1行字符串,代表着老师给的匹配模板。

 

输出格式:

输出合格的字符串的行号和该字符串。(中间以空格隔开)

 

输入样例:

4
Aab
a2B
ab
ABB
a[a2b]b

 

输出样例:

1 Aab
2 a2B
4 ABB
 1 #include <iostream>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <stack>
 5 #include <string>
 6 #include <math.h>
 7 #include <queue>
 8 #include <stdio.h>
 9 #include <string.h>
10 #include <vector>
11 
12 #define lowbit(x) x&(-x)
13 
14 using namespace std;
15 
16 string modu;//模板
17 string hw[55];//同学写的作业
18 int n;
19 
20 bool alphas(char a, char b) {
21     if (a == b)
22         return true;
23     if (a <= 'z' || a >= 'a')
24         if (a + 'A' - 'a' == b)
25             return true;
26     if (a <= 'Z' || a >= 'A')
27         if (a + 'a' - 'A' == b)
28             return true;
29     return false;
30 }
31 
32 bool cmp(string mod, string hw) {
33     int modl = mod.length(), hwl = hw.length(),j=0;
34     for (int i = 0; i < modl;) {
35         if (j >= hwl)//当hw的长度较小时
36             return false;
37         if (alphas(mod[i], hw[j])) {
38             i++,j++;
39             continue;
40         }//当两者无法匹配
41         if (mod[i] == '[') {
42             bool flag = false;
43             while (mod[i] != ']') {
44                 i++;
45                 if (flag)
46                     continue;
47                 if (alphas(mod[i], hw[j]))
48                     flag = true;
49             }
50             i++;
51             if (flag)
52             {
53                 j++;
54                 continue;
55             }
56             else
57                 return false;
58         }//当出现[时
59         else
60             return false;
61     }
62     if (j == hwl)//当两者刚好匹配时,j现在
63         return true;
64     return false;
65 }
66 
67 int main()
68 {
69     scanf("%d\n",&n);
70     for (int i = 1; i <= n; i++)
71         cin >> hw[i];
72     cin >> modu;
73     for (int i = 1; i <= n; i++)
74     {
75         if (cmp(modu, hw[i]))
76             cout << i << " " << hw[i] << endl;
77     }
78     return 0;
79 }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-19
  • 2022-01-01
猜你喜欢
  • 2021-04-17
  • 2022-01-20
  • 2021-06-27
  • 2021-10-29
  • 2022-12-23
  • 2021-08-08
相关资源
相似解决方案