pku 1056(字典樹)

字典樹,做的,據說暴力能過。。。

 

代碼:

#include <iostream>
#include <string>
using namespace std;

struct tree {
    int nxt[2];
    bool isWord;
    void init() {
        nxt[0]=nxt[1] = -1;
        isWord = false;
    }
}num[1000010];

int len;

int main() {

    string str;
    len = 0;
    bool pan = true;
    num[0].init();
    int ca(1);
    while (cin>>str) {
        if (str=="9") {

          if(pan) cout<<"Set "<<ca++<<" is immediately decodable"<<endl;
          else cout<<"Set "<<ca++<<" is not immediately decodable"<<endl;

          len = 0;
          num[0].init();
          pan = true;
        } else {
            int s(0);
            int e = str.size();
            int i(0);
            while (s<e) {

                if (num[i].nxt[str[s]-'0']!=-1) {
                  i = num[i].nxt[str[s]-'0'];
                  if(s==e-1) num[i].isWord = true;
                  if(num[i].isWord) pan = false;

                } else {
                    ++len;
                     num[len].init();
                     num[i].nxt[str[s]-'0'] =len;
                     i = len;
                     if(s==e-1) num[i].isWord = true;

                }
                ++s;
            }

        }

    }
    return 0;
}

發佈了32 篇原創文章 · 獲贊 3 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章