1. 程式人生 > >POJ-1056(Trie)

POJ-1056(Trie)

題意:判斷一組字串中是否有兩個串A和串B,且A是B的字首,通過將串依次插入Trie即可判斷當前串是否是前面串的字首,和前面串是否有當前串的字首。


#include <cstdio>
#define MAX_N	200005

int N, NEX;
struct Node{
	Node* ch[2];
	bool isLeaf(){
		return !ch[0] && !ch[1];
	}
} nodes[MAX_N], *root;
Node* newNode(){
	nodes[NEX].ch[0] = nodes[NEX].ch[1] = NULL;
	return nodes + NEX++;
}

bool insert(Node* p, const char* s)
{
	bool newed = false;
	for(; *s; ++s){
		Node*& q = p->ch[*s - '0'];
		if(!q){
			q = newNode();
			newed = true;
		}
		else if(!newed && q != root && q->isLeaf()) return true;
		p = q;
	}
	return !newed;
}

int main()
{
	int kase = 0;
	char s[12];
	bool foundPrefix;
	while(gets(s)){
		++kase;
		NEX = 0;
		root = newNode();
		foundPrefix = false;
		for(; s[0] != '9' && !foundPrefix; gets(s)) foundPrefix = insert(root, s);
		while(s[0] != '9') gets(s);
		if(foundPrefix) printf("Set %d is not immediately decodable\n", kase);
		else printf("Set %d is immediately decodable\n", kase);
	}
	return 0;
}


相關推薦

POJ-1056Trie

題意:判斷一組字串中是否有兩個串A和串B,且A是B的字首,通過將串依次插入Trie即可判斷當前串是否是前面串的字首,和前面串是否有當前串的字首。 #include <cstdio> #define MAX_N 200005 int N, NEX; stru

Colored Sticks POJ - 2513trie樹歐拉路

ans pll pac urn bitset freopen rep -- algo 題意:   就是無向圖歐拉路 解析:   不能用map。。超時   在判斷是否只有一個聯通的時候,我比較喜歡用set,但也不能用set,會超時,反正不能用stl emm 用trie樹來編號

trieUVALive - 3942 Remember the Word

span map remember efi problem 註意 find 不存在 中間節點 題目鏈接 設d[i]表示從下標i的字符開始的字符串的分解方法數,顯然有倒序的遞推公式。 需要求每個位置開始是否能組成模式串的前綴,才可以建立正確的遞推。 1 #include

trieHDU1251 統計難題

文件 esp 出現 ble nan ont oid 結束 abc Ignatius最近遇到一個難題,老師交給他很多單詞(只有小寫字母組成,不會有重復的單詞出現),現在老師要他統計出以某個字符串為前綴的單詞數量(單詞本身也是自己的前綴). Input輸入數據的第一部分是一張

LibieOJ 6170 字母樹 Trie

spa highlight get blog cnblogs uil tin for log 題目鏈接 字母樹 (以每個點為根遍歷,插入到trie中,統計答案即可)——SamZhang #include <bits/stdc++.h&g

Discrete Logging POJ - 2417BSGS

1.0 can origin hide ret isp namespace 技術分享 ons Discrete Logging POJ - 2417 題意:給P,B,N,求最小的L使得 BL≡N (mod P) Baby Step Giant Step 1 #in

POJ 3414 BFS

class cout () amp esp cst LV pan span 這道題還是比較簡單的,就是寫起來有點麻煩,剛開始做的時候一直一位有什麽簡單方法,拖了好久。。。還是自己分析問題的能力不行啊 1 #include<iostream> 2 #i

POJ 3126BFS

code stream using \n == bfs push string ostream 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #in

Bzoj 1212----L語言Trie

cst 輕松 class 符號 pac itl sub ostream mit Problem 1212. -- [HNOI2004]L語言 1212: [HNOI2004]L語言Time Limit: 10 Sec Memory Limit: 162 MBSubmit:

UVA - 11732 "strcmp()" Anyone? trie

就是 false 計數 pan size int name 總數 lan https://vjudge.net/problem/UVA-11732 題意 給定n個字符串,問用strcmp函數比較這些字符串共用多少次比較。 strcmp函數的實現 int strc

POJ 1068模擬

mat while span argv == 記錄 spa mes ack 用1代表左括號,0代表右括號。掃一遍,掃到0的時候,往前掃,直到在這個情況下0和1的個數相等,記錄。 1 #include <iostream> 2 #include <cs

BZOJ4260 Codechef REBXORtrie

query ems ring efi fine ret 異或 esp memset   用trie求出前綴最大區間異或和、後綴最大區間異或和即可。註意空間是nlog的。 #include<iostream> #include<cstdio> #i

POJ題目

POJ題目(轉) http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期: 一.基本演算法:      (1)列舉. (poj1753,poj2965)  &nbs

POJ——1016Parencodings模擬

題目連結: #include <cstdio> #include <cmath> #include<vector> #include<cstring> #include<algorithm> #include<cmath>

B - Calendar POJ - 2080 曆法

B - Calendar  POJ - 2080  A calendar is a system for measuring time, from hours and minutes, to months and days, and finally to years an

A - Maya Calendar POJ - 1008 曆法

 A - Maya Calendar POJ - 1008  During his last sabbatical, professor M. A. Ya made a surprising discovery about the old Maya calendar. Fro

Balanced Lineup POJ - 3264RMQ

Balanced Lineup POJ - 3264 題目連線 題意:給出一個數列,Q個詢問,問區間[A, B]中最大值與最小值的差; 思路:線段樹可以做,維護最大最小值,直接查詢就可以;但是現在要用RMQ做; 何為RMQ?(Range Minimum/Maximum Que

LOJ6469 Magictrie+

題目連結 紀念我菜的真實的一場模擬賽 首先看到這個題目,一開始就很毒瘤。一定是沒有辦法直接做的。 我們考慮轉化問題 假設,我們選擇列舉 x

Find The Multiple POJ - 1426 BFS

題目大意 給定一個整數,尋找一個只有0,1構成的十進位制數使得這個數能夠整除這個整數 解法 直接bfs第一位放入1,之後每一位放入1或者0 程式碼 #include <iostream> #include <queue> using namespace std; int n;

Humidex POJ - 3299 數學

題目大意 給定你三個變數中的兩個輸出剩下的那一個 題解 沒有什麼,就是把公式推出來即可,完全的數學題 程式碼 #include <iostream> #include <cmath> #include <cstdio> using namespace std;