题解 | #字符串匹配#
字符串匹配
https://www.nowcoder.com/practice/fbdc522ef958455687654b38a4ca01e0
#include <iostream> #include <vector> #include <algorithm> using namespace std; const int N = 1010; string p, s[N]; string tolow(string s){ string res; for(int i = 0; i < s.size(); i ++) res += tolower(s[i]); return res; } bool match(string a, string p){ for(int i = 0, j = 0; i < a.size() || j < p.size(); i ++){ if(i == a.size() || j == p.size()) return false; if(p[j] != '['){ if(p[j] != a[i]) return false; j ++; }else{ string s; j ++; while(p[j] != ']') s += p[j ++]; j ++; if(s.find(a[i]) == -1) return false; } } return true; } int main(){ int n; cin>>n; for(int i = 0; i < n; i ++) cin>>s[i]; cin>>p; for(int i = 0; i < n; i ++){ if(match(tolow(s[i]), tolow(p))) cout<<i + 1<<" "<<s[i]<<endl; } return 0; }