POJ 1056 immediate decodability (Dictionary tree)

Source: Internet
Author: User
Tags acos

 


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 ;}

 

Related Article

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.