HDU1671 Phone List (Dictionary tree)
Question:
If you enter multiple string numbers, you must determine whether some numeric strings are prefix of other strings. If NO output exists, otherwise YES is output.
Solution:
Create a dictionary tree with trie, mark 1 at the end of each digit string, and then judge each input string. Whether there is a flag mark.
# Include
# Include
# Include
# Include
Using namespace std; const int MAX_LEN = 11; typedef struct trie {trie * next [10]; int num ;}t; T * tree; bool flag; void trieDel (T * p) {for (int I = 0; I <10; I ++) {if (p-> next [I]! = NULL) trieDel (p-> next [I]);} free (p);} void trieInsert (char str []) {T * p = tree, * q; int len = strlen (str); for (int I = 0; I
Next [id] = NULL) {q = new T; for (int j = 0; j <10; j ++) q-> next [j] = NULL; q-> num = 0; p-> next [id] = q; p = p-> next [id];} else p = p-> next [id]; if (p-> num = 1) flag = false;} p-> num = 1; for (int I = 0; I <10; I ++) {if (p-> next [I]! = NULL) {flag = false; break ;}} void init () {tree = new T; for (int I = 0; I <10; I ++) {tree-> next [I] = NULL ;}int main () {int cas; scanf ("% d", & cas); while (cas --) {flag = true; int n; scanf ("% d", & n); getchar (); init (); for (int I = 0; I
The following code does not use the dictionary tree:
#include
#include#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef unsigned long long ull;typedef long long ll;using namespace std;string inp[10010];bool solve(const string &a, const string & b){ if(a.length() > b.length()) return false; for(unsigned i = 0; i < a.length(); ++i) if(a[i] != b[i]) return false; return true;}int main(){ int t; cin >>t; while(t--){ int n; bool bol = true; cin >> n; for(int i = 0; i < n; ++i) cin >> inp[i]; sort(inp, inp + n); for(int i = 0; i < n; ++i) if(solve(inp[i], inp[i+1])) bol = false; cout <<(bol?"YES":"NO") <