1 条题解
-
0
思路
可理解前缀需要分词可达性,不是最长匹配词;词长最多10,用AC识别结尾词长和10位历史优化20篇长文。
参考程序(C++11)
#include <bits/stdc++.h> using namespace std; int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int n,m;cin>>n>>m;vector<array<int,26>>go(1);vector<int>fail(1),out(1);while(n--){string s;cin>>s;int v=0;for(char c:s){int z=c-'a';if(!go[v][z]){go[v][z]=go.size();go.push_back({});fail.push_back(0);out.push_back(0);}v=go[v][z];}out[v]|=1<<(s.size()-1);}queue<int>q;for(int c=0;c<26;c++)if(go[0][c])q.push(go[0][c]);while(!q.empty()){int v=q.front();q.pop();out[v]|=out[fail[v]];for(int c=0;c<26;c++)if(go[v][c]){int u=go[v][c];fail[u]=go[fail[v]][c];q.push(u);}else go[v][c]=go[fail[v]][c];}while(m--){string s;cin>>s;int history=1,v=0,best=0;for(int i=0;i<(int)s.size();i++){v=go[v][s[i]-'a'];bool can=history&out[v];history=((history<<1)|can)&1023;if(can)best=i+1;if(!history)break;}cout<<best<<'\n';}}
- 1