输出
对于每个测试数据,如果是一致的输出“YES”,如果不是输出“NO”。
样例输入
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
样例输出
NO
YES
#include <stdio.h> #include <string.h> #include <stdlib.h> struct Node{ struct Node *next[10]; int isCover, isEnd; }; int ok; void clean(Node *p) { memset(p->next, 0, sizeof(p->next)); p->isCover = p->isEnd = 0; } void insert(char *str, Node *root) { Node *p = root; int id; while(*str){ id = *str - '0'; if(p->next[id] == NULL){ p->next[id] = (Node *)malloc(sizeof(Node)); clean(p->next[id]); } p = p->next[id]; if(p->isEnd) ok = 0; ++p->isCover; ++str; } if(p->isCover > 1) ok = 0; p->isEnd = 1; } void DELETE(Node *p) { for(int i = 0; i < 10; ++i) if(p->next[i]) DELETE(p->next[i]); free(p); } int main() { int t, n; char str[12]; scanf("%d", &t); while(t--){ Node *root = (Node *)malloc(sizeof(Node)); scanf("%d", &n); clean(root); ok = 1; while(n--){ scanf("%s", str); if(ok) insert(str, root); } printf(ok ? "YES\n" : "NO\n"); DELETE(root); } return 0; }
原文地址:http://blog.csdn.net/chang_mu/article/details/37941077