/*
合格的字符串
查看 提交 统计 提问
总时间限制: 1000ms 内存限制: 65535kB
描述
老师给小学生门布置了一些作业,让它们按照一个模版写一些字符串交上来,
同学们把作业交上来了,问题来了,这么多的作业老师批改不过来,现在请你帮老师
写一个程序,帮助老师确定各个字符串是否合格。
首先老师有一个匹配模版,比如是“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
提示
被检测的字符串中只有数字和字母。
*/
#include <cstdlib>
#include <string>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
char get_l(char le)//用来将大写字母转为小写字母 便于判断
{
if (le >= 'A' && le <= 'Z') return le - 'A' + 'a';
return le;
}
int main()
{
int n = 0;
string str[52];
int m_q[55][75];//记录第i次匹配的字符组
cin >> n;
for (int i = 0; i < n; ++i)
{
cin >> str[i];
}
string temp_str;
string mark;//转化后的模式串 用问号标记需要匹配的字符位置
cin >> temp_str;//模式串
bool inq = 0; //是否在读入方括号内的内容
int m_cur = 0, q_cur = -1;
int q_poi = 0;
for (int i = 0; i < temp_str.length(); ++i)
{
if(!inq)
{
if ((temp_str[i] >= 'a' && temp_str[i] <= 'z') || (temp_str[i] >= 'A' && temp_str[i] <= 'Z') || (temp_str[i] >= '0' && temp_str[i] <= '9'))
{
mark[m_cur++] = temp_str[i];
}
else if (temp_str[i] == '[')//需要匹配的标志
{
inq = 1;
q_poi = 0;
++q_cur;
}
}
else
{
if ((temp_str[i] >= 'a' && temp_str[i] <= 'z') || (temp_str[i] >= 'A' && temp_str[i] <= 'Z') || (temp_str[i] >= '0' && temp_str[i] <= '9'))
{
m_q[q_cur][++q_poi] = temp_str[i];
}
else if(temp_str[i] == ']')//当前[匹配字符组输入结束的标志
{
m_q[q_cur][0] = q_poi;//m_q首位记录当前匹配字符组的长度
mark[m_cur++] = '?';//需要匹配的位置模式串置为问号
inq = 0;
}
}
}
for (int i = 0; i < n; ++i)
{
bool is_valid = 1;//是否有效
int match_num = -1;//记录进行第几次匹配
if (str[i].length() != m_cur) is_valid = 0;//长度不等直接判为无效
if (!is_valid) continue;
for (int j = 0; j < str[i].length(); ++j)
{
if (!is_valid) break;
if (mark[j] != '?' && get_l(mark[j]) != get_l(str[i][j]))//字母或数字
{
is_valid = 0;
break;
}
else if(mark[j] == '?')//需要匹配
{
++match_num;
char ch_temp = get_l(str[i][j]);
int q_len = m_q[match_num][0];
bool is_cor = 0;
for (int k = 1; k <= q_len; ++k)
{
if (ch_temp == get_l(m_q[match_num][k]))//当前字符与待匹配字符逐个匹配
{
is_cor = 1;
break;
}
}
if (!is_cor)
{
is_valid = 0;
break;
}
}
}
if (is_valid)
{
cout << i + 1 << " " << str[i] << endl;
}
}
return 0;
}
转载于:https://my.oschina.net/locusxt/blog/167510