基本的字典树 判断一组字符串中是否出现自己的前缀子串#include <stdio.h> #include <string.h> const int MAXN = 100010; struct dicTree { int next[10]; bool isWord; void init(){ memset(next,-1,sizeof(next)); isWord = false; } }; dicTree tree[MAXN]; int num; bool ok; void insert(char *s){ int index = 0 ,level = 1; while( *s ){ int i = *s - '0'; if( tree[index].next[i] == -1){ tree[++num].init(); tree[index].next[i] = num; index = num; } else { index = tree[index].next[i]; if( tree[index].isWord == true ){ //判断长度小于等于当前插入单词的结尾 ok = false; return; } } s++; } tree[index].isWord = true; for(int i = 0; i < 10; i++){ //判断单词长度大于当前插入单词的是否已经插入过 if( tree[index].next[i] != -1 ){ ok = false; break; } } } int main(){ int t,n; char telNum[11]; scanf("%d",&t); while( t-- ){ scanf("%d",&n); ok = true; num = 0; tree[0].init(); while( n-- ){ if( ok ){ scanf("%s",telNum); insert(telNum); } else { scanf("%s",telNum); } } if( ok ) printf("YES/n"); else printf("NO/n"); } return 0; }