北航计算机夏令营机试(?)简单字符串匹配
题意:
- 连 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; } }
本文作者:MoonOut
本文链接:https://www.cnblogs.com/moonout/p/16455360.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步