北航计算机夏令营机试(?)简单字符串匹配

题意:

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9+7;
int n; string o[1010], s[1010]; string pattern;
bool match(int index){
int i=0, j=0;
while(i<s[index].length()){
if(pattern[j]=='['){
int k=j+1; while(pattern[k]!=']') ++k;
bool ok=false;
for(int l=j+1; l<k; ++l)
if(pattern[l]==s[index][i]){
ok=true; break;
}
if(!ok) return false;
j=k+1; ++i;
}
else if(pattern[j]!='[' && pattern[j]!=']'){
if(s[index][i]!=pattern[j]){
// cout<<"index, i, j = "<<index<<", "<<i<<", "<<j<<endl;
// cout<<"s[index][i], pattern[j] = "<<s[index][i]<<", "<<pattern[j]<<endl;
return false;
}
++j; ++i;
}
}
return i==s[index].length() && j == pattern.length();
}
int main(){
ios::sync_with_stdio(false);
int n; cin>>n;
for(int i=0; i<n; ++i){
cin>>o[i]; s[i]=o[i];
for(int j=0; j<s[i].length(); ++j)
if(s[i][j]<='Z' && s[i][j]>='A') s[i][j]=char(s[i][j]-'A'+'a');
}
cin>>pattern; for(char xx : pattern)
if(xx<='Z' && xx>='A') xx=char(xx-'A'+'a');
for(int i=0; i<n; ++i){
if(match(i))
cout<<i+1<<" "<<o[i]<<endl;
}
}

本文作者:MoonOut

本文链接:https://www.cnblogs.com/moonout/p/16455360.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   MoonOut  阅读(55)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 Sibelius: Violin Concerto in D Minor, Op. 47:III. Allegro, ma non tanto Jascha Heifetz / Chicago Symphony Orchestra
Sibelius: Violin Concerto in D Minor, Op. 47:III. Allegro, ma non tanto - Jascha Heifetz / Chicago Symphony Orchestra
00:00 / 00:00
An audio error has occurred.