Title Link:http://poj.org/problem?id=3630
Test instructions: a given n string. Determines whether there is a prefix for one of the strings as a different string. If there is no output yes. Otherwise output No.
Idea: a bare dictionary tree.
Code
#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include < Algorithm> #include <string>using namespace std; #define Lson L, M, RT << 1#define Rson m + 1, R, RT << 1 | 1#define ceil (x, Y) (((x) + (y)-1)/(y)) const int SIZE = 10;const int N = 1e5 + 10;const int INF = 0x7f7f7f7f;struct Tr ie {int val[size];int count;//How many child nodes};int sz; Trie Pn[n];int NewNode () {memset (pn[sz].val, 0, sizeof (pn[sz].val));p n[sz].count = 0;return sz++;} void Insert (char *s) {int u = 0;for (int i = 0; s[i]; i++) {int idx = s[i]-' 0 '; if (!pn[u].val[idx]) {Pn[u].val[idx] = NE Wnode ();p n[u].count++;} U = Pn[u].val[idx];}} BOOL Findpre (char *s) {int u = 0;for (int i = 0; s[i]; i++) {int idx = s[i]-' 0 '; if (!pn[u].val[idx]) {if () r Eturn True;elsereturn false;} U = pn[u].val[idx];} return true;} int main () {int t_case;scanf ("%d", &t_case), for (int i_case = 1; i_case <= t_case; i_case++) {int n;scanf ("%d", &am p;n); Char Str[size];boolFlag = True;sz = 0;newnode (), scanf ("%s", str), insert (str), for (int i = 1; i < n; i++) {scanf ("%s", str); if (Findpre (str ) flag = False;insert (str);} if (flag) puts ("YES"); Elseputs ("NO");} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 3630 Phone List (dictionary tree)