POJ 3630 Phone List (dictionary tree)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.