九度上能做出来这道题北航考研上机真题的不多,我半个小时就一次AC了,小有成就感。
题目URL:http://ac.jobdu.com/problem.php?id=1165
这道题有些正则表达式匹配的感觉,可是不知道九度的编译器支不支持C++标准库的regular_expression库。反正就用基本的C++写了,练练字符串处理的基本功。
题目说大小写通配,所以我干脆把所有的字符串全部转换成小写的,数字则不变,以方便后面匹配。匹配时做了个小技巧,分开匹配,先匹配中括号前面的,再匹配中间的单个字符,最后匹配中括号后面的。
我的AC代码:
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
const int Max = 1010;
string s[Max];
string con[Max];
int main()
{
int n;
string temp, pre, middle, post;
int p1, p2;
while(scanf("%d", &n) != EOF)
{
for(int i=0; i<n; ++i)
{
cin >> s[i];
con[i] = s[i];
for(int j=0; j<s[i].size(); ++j)
if(s[i][j] >= 'a') con[i][j] -= 32;
}
cin >> temp;
for(int j=0; j<temp.size(); ++j)
if(temp[j] >= 'a') temp[j] -= 32;
p1 = temp.find('[');
if(p1 == string::npos)
{
for(int i=0; i<n; ++i)
if(s[i] == temp) cout << i+1 << ' ' << s[i] << endl;
}
else
{
p2 = temp.find(']');
pre = temp.substr(0, p1);
middle = temp.substr(p1+1, p2-p1-1);
post = temp.substr(p2+1);
for(int i=0; i<n; ++i)
if(con[i].substr(0, p1) == pre
&& middle.find(con[i][p1]) != string::npos
&& con[i].substr(p1+1) == post)
cout << i+1 << ' ' << s[i] << endl;
}
}
system("pause");
return 0;
}