Before I did not use hash, has been tle, string processing time is too long, after using the hash has been CE, (see) I have been through my dictionary tree G++mle,c++ac , has been naïve with C + +, and later CE is because of this, g++ only support this hash ...
#include <cstdio>
#include <iostream> #include<string.h>inthash[ -];structtrienode{intNo; Trienode*next[Ten];} node[30005]; Trienode*root = &node[0];intcnt,result[5005];Charword[Ten],s[Ten];voidinit () {hash['a']=hash['b']=hash['C']=2; hash['D']=hash['e']=hash['F']=3;
hash['g']=hash['h']=hash['I']=4; hash['J']=hash['k']=hash['L']=5; hash['m']=hash['N']=hash['o']=6; hash['P']=hash['Q']=hash['R']=hash['s']=7; hash['T']=hash['u']=hash['v']=8; hash['W']=hash['x']=hash['y']=hash['Z']=9;}voidInitroot ()
{ inti; for(i=0; i<Ten; i++) {root->next[i]=NULL; }}voidInsertCharStr[],intnum) {Trienode*p =Root; intlen=strlen (str), i,j; for(i=0; i<len; i++)
{ if(p->next[str[i]-'0']==NULL) {P->next[str[i]-'0']=&node[cnt]; for(j=0; j<Ten; J + +) node[cnt].next[j]=NULL; Node[cnt].no=-1; CNT++; } P=p->next[str[i]-'0']; } P->no=num;}/** Query the number string corresponding to a letter string*/voidQueryCharstr[]) { intlen=strlen (str), I; Trienode*p=Root; for(i=0; i<len; i++) {
P=p->Next[hash[str[i]]; if(P==null) Break; } if(P==null)return; Else {
if(p->no!=-1) result[p->no]++; }}intMain () {intT,m,n,i;
scanf ("%d",&t); Init ();
while(t--) {CNT=1; Initroot (); memset (Result,0,sizeof(result));
scanf ("%d%d",&n,&m); for(i=0; i<n; i++) {scanf ("%s", Word); Insert (word,i);
} for(i=0; i<m; i++) {scanf ("%s", s); query (s); } for(i=0; i<n; i++)
{printf ("%d\n", Result[i]); } } return 0;}
HDU 4287 Intelligent IME