北航计算机夏令营机试(?)简单字符串匹配
题意:
- 连 dfs 都不用的匹配,请参见:https://www.noobdream.com/DreamJudge/Issue/page/1378/
代码:
#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;
}
}

浙公网安备 33010602011771号