Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=1671
Main topic:
Give you n a string to determine if there is a string in the N string that is the prefix of another string, if it exists
Output "NO", otherwise output "YES".
Ideas:
Create a dictionary tree that stores n strings in a dictionary tree Count the number of prefix occurrences. Find the N string again, if
The number of occurrences of the string >1, indicating that the repetition occurred two times, the output "NO". If each appears, the output is "YES".
This problem if every time do not delete the dictionary tree, clear space, will be super memory. So add the operation to empty the dictionary tree.
AC Code:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace Std;char s[10010][11];struct trienode{int Count; struct Trienode *next[10];}; Trienode *root;void Create () {root = new Trienode; memset (root->next,null,sizeof (Root->next)); Root->count = 0;} void Insert (char *s) {Trienode *p, *q; p = root; while (*s) {if (p->next[*s-' 0 '] = = NULL) {q = new Trienode; memset (q->next,null,sizeof (Q->next)); Q->count = 1; p->next[*s-' 0 '] = q; } else p->next[*s-' 0 ']->count++; p = p->next[*s-' 0 ']; s++; }}int Find (char *s) {Trienode *p; p = root; while (*s) {if (p->next[*s-' 0 '] = = NULL) return 0; p = p->next[*s-' 0 ']; s++; } return p->count;} int Dele (Trienode *p) {if (p = = NULL) return 0; for (int i = 0; i < ++i) if (P-> Next[i]! = NULL) Dele (P->next[i]); Free (p);} int main () {int t,n; scanf ("%d", &t); while (t--) {scanf ("%d", &n); Create (); for (int i = 0; i < N; ++i) {scanf ("%s", S[i]); Insert (S[i]); } int flag = 1; for (int i = 0; i < N; ++i) {if (Find (S[i]) > 1) flag = 0; } if (flag) printf ("yes\n"); else printf ("no\n"); Dele (root); } return 0;}
HDU1671 Phone List "Dictionary tree"