Here are some strings for you to determine if one of them is the prefix of another string.
Solution: dictionary tree solution.
AC code:
# Include <iostream> # include <cstdio> # include <cstring> # include <string> # include <cstdlib> # include <cmath> # include <vector> # include <list> # include <deque> # include <queue> # include <iterator> # include <stack> # include <map> # include <set> # include <algorithm> # include <cctype> using namespace std; typedef _ int64 LL; const int N = 2; const LL mod = 000000007ll; const int INF = 0x3f3f3f3f; const d Ouble PI = acos (-1.0); char s [15]; struct Trie {Trie * next [N]; int v; Trie () {v = 1; for (int I = 0; I <N; I ++) next [I] = NULL ;}} * root; void setroot () {root = (Trie *) malloc (sizeof (Trie); for (int I = 0; I <N; I ++) root-> next [I] = NULL ;} void createTrie (char * str) {int I, j, id; int len = strlen (str); Trie * p = root, * m; for (I = 0; I <len; I ++) {id = str [I]-'0'; if (p-> next [id] = NULL) {p-> next [id] = new Trie (); p = p-> next [id ];} Else {p = p-> next [id];} p-> v ++;} int findTrie (char * str) {int I, id; int len = strlen (str); Trie * p = root; for (I = 0; I <len; I ++) {id = str [I]-'A '; p = p-> next [id]; if (p = NULL) // if it is an empty set, return 0 is not saved as the prefix;} return p-> v; // This string is the prefix of a string in the character set. You may need to determine whether it is at the End Node} void dealTrie (Trie * T) {// if multiple groups of data exist-multiple trees, then release the inner pure int I; if (T = NULL) return; for (I = 0; I <N; I ++) if (T-> next [I]! = NULL) dealTrie (T-> next [I]); free (T); // release} int flag; void dfs (Trie * Tr) {if (flag) return; if (Tr-> v> 1 & (Tr-> next [0]! = NULL | Tr-> next [1]! = NULL) {flag = 1; return ;}for (int I = 0; I <2; I ++) {if (Tr-> next [I] = NULL) continue; dfs (Tr-> next [I]) ;}} int main () {int I, j, p = 1, ca = 0; while (scanf ("% s", s )! = EOF) {if (p) root = new Trie (); if (strcmp (s, "9 ")! = 0) {createTrie (s); p = 0;} else {flag = 0; dfs (root); if (flag) printf ("Set % d is not immediately decodable \ n", ++ ca); else printf ("Set % d is immediately decodable \ n", ++ ca ); dealTrie (root); p = 1 ;}} return 0 ;} # include <iostream> # include <cstdio> # include <cstring> # include <string> # include <cstdlib> # include <cmath> # include <vector> # include <list> # include <deque> # include <queue> # include <iterator> # inc Lude <stack> # include <map> # include <set> # include <algorithm> # include <cctype> using namespace std; typedef _ int64 LL; const int N = 2; const LL mod = rj00007ll; const int INF = 0x3f3f3f3f; const double PI = acos (-1.0); char s [15]; struct Trie {Trie * next [N]; int v; Trie () {v = 1; for (int I = 0; I <N; I ++) next [I] = NULL ;}} * root; void setroot () {root = (Trie *) malloc (sizeof (Trie); for (int I = 0; I <N; I ++) root-> next [I] = N ULL;} void createTrie (char * str) {int I, j, id; int len = strlen (str); Trie * p = root, * m; for (I = 0; I <len; I ++) {id = str [I]-'0'; if (p-> next [id] = NULL) {p-> next [id] = new Trie (); p = p-> next [id];} else {p = p-> next [id];} p-> v ++;} int findTrie (char * str) {int I, id; int len = strlen (str); Trie * p = root; for (I = 0; I <len; I ++) {id = str [I]-'A'; p = p-> next [id]; if (p = NULL) // if it is an empty set, return 0 is not saved as the prefix;} return p-> v; // This string is Prefix of a string in the character set. You may need to determine whether it is at the End Node} void dealTrie (Trie * T) {// if multiple groups of data exist-multiple trees, then release the inner pure int I; if (T = NULL) return; for (I = 0; I <N; I ++) if (T-> next [I]! = NULL) dealTrie (T-> next [I]); free (T); // release} int flag; void dfs (Trie * Tr) {if (flag) return; if (Tr-> v> 1 & (Tr-> next [0]! = NULL | Tr-> next [1]! = NULL) {flag = 1; return ;}for (int I = 0; I <2; I ++) {if (Tr-> next [I] = NULL) continue; dfs (Tr-> next [I]) ;}} int main () {int I, j, p = 1, ca = 0; while (scanf ("% s", s )! = EOF) {if (p) root = new Trie (); if (strcmp (s, "9 ")! = 0) {createTrie (s); p = 0;} else {flag = 0; dfs (root); if (flag) printf ("Set % d is not immediately decodable \ n", ++ ca); else printf ("Set % d is immediately decodable \ n", ++ ca ); dealTrie (root); p = 1 ;}} return 0 ;}