哈希算法。各串存入哈希值,再列举各串的断点,O(1)更新哈希值。
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <iomanip> #include <cstring> #include <map> #include <queue> #include <set> #include <cassert> #include <stack> #include <bitset> //#include <unordered_set> #define mkp make_pair #define err cout<<"here"<<endl using namespace std; const double EPS=1e-12; typedef long long lon; typedef unsigned long long ull; typedef map<ull,int>::iterator IT; const lon SZ=50010,SSZ=1010,APB=26,mod=20071027,one=97; const lon INF=0x7FFFFFFF; int n; struct nd{ lon to,wt; nd(lon a=0,lon b=0):to(a),wt(b){} }; string str[SZ]; ull ft[SZ]; set<ull> st; void add(int x) { ull hs=0; for(int i=0;i<str[x].size();++i) { hs=hs*one+str[x][i]; } st.insert(hs); //cout<<hs<<endl; } bool find(ull x) { return st.find(x)!=st.end(); } bool chk(int x) { ull hs1=0,hs2=0; for(int i=0;i<str[x].size();++i) { hs2=hs2*one+str[x][i]; } int len=str[x].size(); for(int i=0;i<str[x].size();++i) { hs1=hs1*one+str[x][i]; hs2-=ft[len-i-1]*str[x][i]; if(find(hs1)&&find(hs2))return 1; } return 0; } void init() { for(n=0;cin>>str[++n];) { add(n); } ft[0]=1; for(int i=1;i<SZ;++i)ft[i]=ft[i-1]*one; for(int i=1;i<=n;++i) { if(chk(i))cout<<str[i]<<endl; } } void work() { } void release() { //memset(dp,0,sizeof(dp)); } int main() { std::ios::sync_with_stdio(0); //freopen("d:\\1.txt","r",stdin); lon casenum; //cin>>casenum; //cout<<casenum<<endl; //for(int time=1;time<=casenum;++time) //for(lon time=1;cin>>str+1;++time) { //if(time!=1)cout<<endl; init(); work(); release(); } return 0; }