扔到一个Trie上,分别统计每个节点的答案
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<functional> #include<iostream> #include<cmath> #include<cctype> #include<ctime> #include<queue> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define ForD(i,n) for(int i=n;i;i--) #define RepD(i,n) for(int i=n;i>=0;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define Forpiter(x) for(int &p=iter[x];p;p=next[p]) #define Lson (x<<1) #define Rson ((x<<1)+1) #define MEM(a) memset(a,0,sizeof(a)); #define MEMI(a) memset(a,127,sizeof(a)); #define MEMi(a) memset(a,128,sizeof(a)); #define INF (2139062143) #define F (100000007) #define MAXNode (MAXN*MAXLen) #define Sigma_size (2) #define MAXN (50000+10) #define MAXLen (200+10) #define MAXT (20+10) typedef long long ll; ll mul(ll a,ll b){return (a*b)%F;} ll add(ll a,ll b){return (a+b)%F;} ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;} void upd(ll &a,ll b){a=(a%F+b%F)%F;} class Trie { public: int ch[MAXNode][Sigma_size]; int v[MAXNode],siz; Trie(int _siz=0):siz(_siz){ans=0; MEM(ch) MEM(v)} void mem(int _siz=0){siz=_siz; ans=0; MEM(ch) MEM(v) } int idx(char c){return c-'0';} void insert(char *s,int val=0) { int u=0,n=strlen(s); Rep(i,n) { int c=idx(s[i]); if (!ch[u][c]) { ++siz; MEM(ch[siz]); ch[u][c]=siz; } u=ch[u][c]; } v[u]+=val; } void find(char *s) { int u=0,n=strlen(s); Rep(i,n) { int c=idx(s[i]); if (!ch[u][c]) { return; } u=ch[u][c]; } } ll ans; void calc(int u,ll l) { Rep(c,Sigma_size) { if (!ch[u][c]) continue; calc(ch[u][c],l+1); v[u]+=v[ch[u][c]]; } ans=max(ans,v[u]*l); } }S; int T,n; char s[MAXLen]; int main() { // freopen("uva11488.in","r",stdin); cin>>T; while(scanf("%d",&n)==1) { S.mem(); For(i,n) { scanf("%s",s); S.insert(s,1); } S.calc(0,0); cout<<S.ans<<endl; } return 0; }
UVA 11488(Hyper Prefix Sets-Trie统计)
原文:http://blog.csdn.net/nike0good/article/details/44893569