标签:des style blog color io os ar java for
Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 130107/65536 K (Java/Others)
Total Submission(s): 456 Accepted Submission(s): 149
#include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; const int maxnode = 2396744; typedef pair<int,int> pii; //const int maxnode = 1000000; const int sigma_size = 8; struct Trie{ int ch[maxnode][sigma_size]; int fa[maxnode]; char let[maxnode]; int sz; void init() { sz=1; memset(ch[0],0,sizeof(ch[0])); } int idx(char c) {return c-‘A‘;} void insert(char *s,int n) { int u=0; for(int i=0;i<n;++i) { int c=idx(s[i]); if(!ch[u][c]) { memset(ch[sz],0,sizeof(ch[sz])); fa[sz]=u; let[sz]=c+‘A‘; ch[u][c]=sz++; if(sz>=maxnode) cout<<"fuck this memory"<<endl; } u=ch[u][c]; } } char ans[10]; int ansz; void bfs() { int u; queue<int> q; q.push(0); while(!q.empty()) { u=q.front(); q.pop(); for(int i=0;i<sigma_size;++i) { if(ch[u][i]) { q.push(ch[u][i]); } else {//find // cout<<"find "<<u<<endl; ans[0]=i+‘A‘; ansz=1; print(u); return; } } } } void print(int u) { while(u) { // cout<<"."; ans[ansz++]=let[u]; u=fa[u]; } // cout<<"sz="<<ansz<<endl; for(int i=ansz-1;i>=0;--i) printf("%c",ans[i]); printf("\n"); } void text() { int i; for(i=0;i<sz;++i) { printf("%d: fa=%d let=%c\n",i,fa[i],let[i]); } } }; char ms[1000005]; Trie tree; void run() { int i,len; scanf("%s",ms); len = strlen(ms); tree.init(); for(i=0;i<len;++i) { tree.insert(ms+i,min(8,len-i)); } // tree.text(); tree.bfs(); } int main() { //freopen("in","r",stdin); int _; scanf("%d",&_); while(_--) run(); return 0; }
hdu4886 TIANKENG’s restaurant(Ⅱ) (trie树或者模拟进制)
标签:des style blog color io os ar java for
原文地址:http://www.cnblogs.com/someblue/p/4018996.html