题目描述:
读入数据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
void testd(std::vector<std::string> col) { std::string str=*col.rbegin();//最后的匹配字符串 int begin=str.find_first_of('['); int last=str.find_first_of(']'); int lb=last-begin; string strtemp=str.substr(begin+1,lb-1); string str1=str.substr(0,begin); std::vector<std::string> coltemp; string str2=str.substr(last+1,str.length()-last-1); for(int i=0;i<lb-1;i++) { string str1temp=str1; string temp1=strtemp.substr(i,1); str1temp.append(temp1); str1temp.append(str2); coltemp.push_back(str1temp); } std::vector<std::string>::iterator iter=coltemp.begin(); std::vector<std::string>::iterator iter1=col.begin(); int j; for(;iter!=coltemp.end();iter++) { for(j=0,iter1=col.begin();iter1!=col.end();iter1++,j++) { char str1[100],str2[100]; strcpy(str1,iter->c_str()); strcpy(str2,iter1->c_str()); strlwr(str1); strlwr(str2); if(!strcmp(str1,str2)) { cout<<j+1<<" "<<iter1->c_str()<<endl; } } } }
int main() { /*time_t now; struct tm* fmt; int i; cin>>i; std::vector<std::string> col; ++i; while(i) { std::string str; cin>>str; col.push_back(str); i--; } time(&now); fmt=localtime(&now); cout<<fmt->tm_sec<<endl; testd(col); time(&now); fmt=localtime(&now); cout<<fmt->tm_sec<<endl;*/ }将字符转化为大写和小写的操作
void toUpper(char* str) { int i=0; while(str[i]!='\0') { if('a'<=str[i]&&str[i]<='z') { str[i]=str[i]+'A'-'a'; } i++; } } void toLower(char *str) { int i=0; while(str[i]!='\0') { if('A'<=str[i]&&str[i]<='Z') { str[i]=str[i]+'a'-'A'; } i++; } }
char strtemp[]="Hello MMM"; cout<<strtemp<<endl; toLower(strtemp); cout<<strtemp<<endl; toUpper(strtemp); cout<<strtemp<<endl;