Poj 1056 trie tree determines if the Harman encoding is legal

Source: Internet
Author: User

After understanding the trie tree, we can implement 1A. In fact, it is estimated that this question can be used as a result, and advanced data may not be required.

First paste the Code Template of Jilin University

/* ===================================================== =============== * | Trie tree (K cross) | init: Init (); | Note: If tree [I] [TK]> 0 indicates that a word exists, you can also give it more meanings; \ * =================================================== ============= */const int TK = 26, TB = 'a'; // TK cross; Start letter: Tb; int top, tree [N] [TK + 1]; // n: Maximum node count top: number of nodes in the first layer void Init () {Top = 1; memset (tree [0], 0, sizeof (tree [0]);} int sear (char * s) {// returns 0 for (int rt = 0; RT = tree [RT] [* s-TB];) if (* (++ S) = 0) return tree [RT] [TK]; // tree [RT] [TK] = rank; return 0 ;} void insert (char * s, int Rank = 1) {int RT, NXT; For (RT = 0; * s; RT = NXT, ++ S) {NXT = tree [RT] [* s-TB]; If (0 = NXT) {// insert tree [RT] [* s-TB] = NXT = top; memset (tree [Top], 0, sizeof (tree [Top]); top ++;} tree [RT] [TK] = rank; // 1 indicates that 0 indicates that no one exists, you can also give it another meaning} void delt (char * s) {// only mark, assuming s must exist int RT = 0; For (; * s; ++ S) RT = tree [RT] [* s-TB]; // The value of the tree itself points to the next node tree [RT] [TK] = 0;} int prefix (char * s) {// longest prefix int RT = 0, lv; For (Lv = 0; * s; ++ S, ++ LV) {RT = tree [RT] [* s-TB]; If (RT = 0) break;} return lv ;}


In fact, I found that the data for this question is too weak., and all the codes have been sorted by length, so I can directly AC


I modified the template and then got a poj1056.

# Include <cstdio> # include <cstring> # include <iostream> using namespace STD; const int n = 2000 + 10; char STR [N]; const int TK = 26, TB = '0'-5; // TK cross; Start letter: Tb; int top, tree [N] [TK + 1]; // n: Maximum node count top: number of nodes in the first layer void Init () {Top = 1; memset (tree [0], 0, sizeof (tree [0]);} int insert (char * s, int Rank =-2) {//-2 indicates the ending int RT, NXT; For (RT = 0; * s; RT = NXT, ++ S) of a word) {NXT = tree [RT] [* s-TB]; If (tree [RT] [TK] <0) Return-2; // This is where I changed it. The original template, in tree [RT] [TK] = rank; indicates the end (equivalent to a null character) // check whether the previous word ends at the end of each step. If yes, it indicates that all the previous words match. Of course, it is not legal if (0 = NXT) {// insert tree [RT] [* s-TB] = NXT = top; memset (tree [Top], 0, sizeof (tree [Top]); top ++;} tree [RT] [TK] = rank; // 1 indicates that 0 indicates that no one exists, you can also give it other meanings: Return 0;} int main () {int ans = 0, CNT = 1, flag = 0; Init (); While (~ Scanf ("% s", STR) & strcmp (STR, "9") {ans = Flag = 0; ans = insert (STR); If (ANS <0) flag = 1; while (scanf ("% s", STR) & strcmp (STR, "9") {ans = insert (STR); If (ANS <0) flag = 1 ;}if (FLAG) {printf ("set % d is not immediately decodable \ n", CNT ++ );} else {printf ("set % d is immediately decodable \ n", CNT ++);} Init ();}}

In fact, this set of data in my code cannot survive.


10
010

01

0000
9

The question does not seem to have been sorted by length, but the test data must have been sorted.

Add a judgment. If * (S + 1) = 0 & NXT! = 0, it indicates that there is a longer string than the one currently being inserted. The currently inserted string is the prefix already inserted...


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.