首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Trie树 运用 Phone List

2012-08-26 
Trie树 应用 Phone List#includecstdio#includecstringusing namespace stdconst int MAXNODE500000

Trie树 应用 Phone List
#include<cstdio> #include<cstring> using namespace std; const int MAXNODE=500000; const int BRANCH=10; int Tree[MAXNODE][BRANCH],SIZE; bool Key[MAXNODE]; bool Insert(char *str) { int Node=0;bool tof=false; for (int i=0;str[i];i++){ int c=str[i]-'0'; if(Tree[Node][c]==-1){ Tree[Node][c]=++SIZE;tof=true; memset(Tree[SIZE],-1,sizeof(Tree[SIZE])); } if(Key[Node]) return false; Node=Tree[Node][c]; } Key[Node]=true; return tof; } void Trie(){ memset(Tree[0],-1,sizeof(Tree[0])); SIZE=0; } char str[15]; int main() { int t,n;bool tof; scanf("%d",&t); while(t--){ memset(Key,false,sizeof(Key)); Trie();tof=true; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%s",str); if(tof){ tof=Insert(str); } } if(tof) puts("YES"); else puts("NO"); } }?

热点排行