Phone list
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:18122 |
|
Accepted:5769 |
Description
Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogue listed these numbers:
- Emergency 911
- Alice 97, 625, 999
- Bob 91 12 54 26
In this case, it's not possible to call Bob, because the central wocould direct your call to the emergency line as soon as you had dialled the first three digits of Bob's phone number. so this list wocould not be consistent.
Input
The first line of input gives a single integer, 1 ≤T≤ 40, the number of test cases. Each test case startsN, The number of phone numbers, on a separate line, 1 ≤N≤ 10000. Then followsNLines with one unique phone
Number on each line. A phone number is a sequence of at most ten digits.
Output
For each test case, output "yes" if the list is consistent, or "no" otherwise.
Sample Input
2391197625999911254265113123401234401234598346
Sample output
NOYES
Source
Nordic 2007
Algorithm: Getting started with trie Prefix Tree
Ideas:1. Trie tree version[Lrj getting started with algorithm competitions, classic training guide P208-209]
Each time you input a string, it is inserted into the trie tree.
When you insert an edge, the following three conditions may occur:
1. The currently inserted string has never been inserted. If false is returned, the inserted string is successfully inserted and the Next string is inserted.
2. The currently inserted string is the prefix of the already inserted string. Stop inserting and return true to output No.
3. the prefix of the currently inserted characters has been inserted as a separate string. Stop inserting and return true to output No.
2. c ++ STL version of OrcThe idea is simple and the code is clear.
Use Vector <string> to store each input string.
Sort by sort in ascending order
Finally, traverse the string to check whether the previous one can find ().
[Trie tree version]
3630 |
Accepted |
2576 K |
125 Ms |
C ++ |
1421b |
2013-04-18 19:21:01 |
A |
Accepted |
2576 KB |
110 MS |
C ++ |
1282 B |
2013-04-18 18:54:47 |
# Include <stdio. h> # include <string. h> const int maxnode = 10000*10 + 10; struct trie {int ch [maxnode] [10]; int Val [maxnode]; int SZ; void Init () {SZ = 1; memset (CH [0], 0, sizeof (CH [0]);} bool insert (char * s, int V) {int Len = strlen (s); int u = 0; int ID, I; for (I = 0; I <Len; I ++) {id = s [I]-'0'; If (! Ch [u] [ID]) {Val [SZ] = 0; memset (CH [SZ], 0, sizeof (CH [SZ]); ch [u] [ID] = SZ ++;} u = CH [u] [ID]; If (Val [u]) return true; // The prefix of the currently inserted s has been separately inserted into else if (I = (len-1 )) {// If the currently inserted s is the prefix of the inserted string for (Int J = 0; j <= 9; j ++) if (CH [u] [J]) return true;} // else if (I = (len-1 )&&! Val [u]) return true;} Val [u] = V; return false; // inserted successfully} trie; int main () {int T; scanf ("% d", & T); While (t --) {int N; char STR [11]; bool flag = true; scanf ("% d ", & N); trie. init (); While (n --) {scanf ("% s", STR); // judge when inserting. If a conflict exists, exit if (trie. insert (STR, 1) {flag = false; break ;}} if (n> 1) while (n --) scanf ("% s", STR ); if (FLAG) printf ("Yes \ n"); else printf ("NO \ n");} return 0 ;}
[C ++ STL version of Orc]
Accepted |
848 K |
438 Ms |
C ++ |
714b |
2013-04-18 00:56:21 |
#include<string>#include<vector>#include<algorithm>#include<iostream>using namespace std;int main(){ int T; cin>>T; while(T--) { int n; vector<string> v; string s; cin>>n; v.clear(); for(int i = 0; i < n; i++) { cin>>s; v.push_back(s); } sort(v.begin(), v.end()); bool flag = true; for(int i = 0; i < n-1; i++) { if(v[i+1].find(v[i]) == 0) { flag = false; break; } } if(flag) cout<<"YES\n"; else cout<<"NO"<<endl; } return 0;}