Poj-3630-Phone List

Source: Internet
Author: User

Question: give n phone numbers (only composed of digits 0-9) and ask if one number is the prefix of another number (1 ≤ Number of test groups t ≤ 40, 1 ≤ n ≤ 10000 ). Question link: http://poj.org/problem? Id = 3630 --> This question is too speechless... Obviously, it's just a Trip, but it's all about TLE until the end of the game. The reason is too much... Pointer write Trip... The static array is used to write the Trip...

# Include <cstdio> # include <cstring> # include <queue> using namespace std; const int maxw = 10 + 5; const int maxc = 100000 + 10; char p [maxw]; bool OK, isp [maxc]; int ch [maxc] [10]; struct Trip {int sz; Trip () {sz = 1; memset (isp, 0, sizeof (isp); memset (ch, 0, sizeof (ch);} int idx (char c) {return c-'0 ';} void insert (char * s) {int len = strlen (s), I; int u = 0; for (I = 0; I <len; I ++) {int c = Idx (s [I]); if (! Ch [u] [c]) ch [u] [c] = sz ++; else {if (I = len-1) {OK = 0; // The current phone number is another phone prefix break;} if (isp [ch [u] [c]) {OK = 0; // In addition, the phone number is the current phone prefix break;} u = ch [u] [c];} isp [u] = 1;} void solve () {if (OK) puts ("YES"); else puts ("NO") ;}}; int main () {int t, n; scanf ("% d ", & t); while (t --) {OK = 1; scanf ("% d", & n); Trip trip; for (int I = 0; I <n; I ++) {scanf ("% s", p); if (OK) trip. insert (p);} trip. solve ();} return 0 ;}

 


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.