标签:des style class blog code java
a ahat hat hatword hziee word
ahat hatword
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <string> #include <algorithm> #include <queue> using namespace std; vector<string> vs,ans; const int maxn = 5000000; int ch[maxn][26]; int cnt; int val[maxn]; int idx(char a){ return a-'a'; } void insert(string st){ int u = 0; for(int i = 0; i < st.size(); i++){ int k = idx(st[i]); if(!ch[u][k]){ val[cnt] = 0; memset(ch[cnt],0,sizeof ch[cnt]); ch[u][k] = cnt++; } u = ch[u][k]; } val[u] = 1; } bool isprefix(string st){ int u = 0; for(int i = 0; i < st.size(); i++){ int k = idx(st[i]); if(!ch[u][k]) return false; u = ch[u][k]; } return val[u]; } int main(){ cnt = 1; memset(ch[0],0,sizeof ch[0]); memset(val,0,sizeof val); string str; vs.clear(); ans.clear(); while(cin >> str){ insert(str); vs.push_back(str); } for(int i = 0; i < vs.size(); i++){ for(int j = 0; j < vs[i].size(); j++){ string t1 = vs[i].substr(0,j+1),t2 = vs[i].substr(j+1); //cout<<t1<<" "<<t2<<endl; if(isprefix(t1)&&isprefix(t2)){ ans.push_back(vs[i]); break; } } } sort(ans.begin(),ans.end()); for(int i = 0; i < ans.size(); i++){ cout<<ans[i]<<endl; } return 0; }
HDU1247-Hat’s Words(trie树),布布扣,bubuko.com
标签:des style class blog code java
原文地址:http://blog.csdn.net/mowayao/article/details/32337295