POJ 3630 Phone List(字典树)

题目链接:http://poj.org/problem?id=3630

题意:给定n个字符串。判断是否存在其中某个字符串为另外一个字符串的前缀。若不存在输出YES。否则输出NO。

思路:裸的字典树。

代码

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <string>

using namespace std;

#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define ceil(x, y) (((x) + (y) - 1) / (y))

const int SIZE = 10;
const int N = 1e5 + 10;
const int INF = 0x7f7f7f7f;

struct Trie {
	int val[SIZE];
	int count;//有多少个子节点
};

int sz;
Trie pn[N];

int newnode() {
	memset(pn[sz].val, 0, sizeof(pn[sz].val));
	pn[sz].count = 0;
	return sz++;
}

void insert(char *s) {
	int u = 0;
	for (int i = 0; s[i]; i++) {
		int idx = s[i] - '0';
		if (!pn[u].val[idx]) {
			pn[u].val[idx] = newnode();
			pn[u].count++;
		}
		u = pn[u].val[idx];
	}
}

bool findpre(char *s) {
	int u = 0;
	for (int i = 0; s[i]; i++) {
		int idx = s[i] - '0';
		if (!pn[u].val[idx]) {
			if (!pn[u].count)
				return true;
			else
				return false;
		}
		u = pn[u].val[idx];
	}
	return true;
}

int main() {

	int t_case;
	scanf("%d", &t_case);
	for (int i_case = 1; i_case <= t_case; i_case++) {
		int n;
		scanf("%d", &n);
		char str[SIZE];
		bool flag = true;
		sz = 0;
		newnode();
		scanf("%s", str);
		insert(str);
		for (int i = 1; i < n; i++) {
			scanf("%s", str);
			if (findpre(str))
				flag = false;
			insert(str);
		}
		if (flag)
			puts("YES");
		else
			puts("NO");
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-04 18:53:59

POJ 3630 Phone List(字典树)的相关文章

poj 3630 Phone List (字典树 +静态字典树)

Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22235   Accepted: 6885 Description Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogu

poj 3630 Phone List(字典树)

题目链接: http://poj.org/problem?id=3630 思路分析: 求在字符串中是否存在某个字符串为另一字符串的前缀: 即对于某个字符串而言,其是否为某个字符串的前缀,或存在某个其先前的字符串为其前缀: (1)若该字符串为某个字符串前缀,则存在一条从根节点到该字符串的最后一个字符串的路径; (2)若存在某个字符串为该字符串前缀,则在该字符串的查找路径中存在一条子路径,路径的最后的结点的endOfWord 标记为true,表示存在某个字符串为其前缀: 代码: #include <

poj 2503:Babelfish(字典树,经典题,字典翻译)

Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have

POJ训练计划_Colored Sticks(字典树+判断欧拉通路)

解题报告 http://blog.csdn.net/juncoder/article/details/38236333 题目传送门 题意: 问给定一堆的棒,两端有颜色,相同的颜色的棒可以头尾相接,能否连在一条直线. 思路: 把每一根棒两端看成两个点,之间连着线,判断这样的一个图中是否有欧拉通路 欧拉通路: 在联通无向图中,经过G的每一条边一次并且仅有一次的路径为欧拉通路. 求欧拉通路的充分条件:图为联通图,并且仅有两个奇度数的结点或无奇度结点. #include <queue> #includ

POJ 2513 Colored Sticks(字典树+并查集连通性+欧拉回路)

题目地址:POJ 2513 刚开始没想到字典树,用的map函数一直TLE,由于上一次的签到题由于没想到字典树而卡了好长时间的深刻教训,于是过了不久就想起来用字典树了,(为什么是在TLE了5次之后..T^T)然后把map改成了字典树,然后就过了. 这题居然不知不觉的用上了欧拉回路..其实当时我是这样想的..因为相互接触的必须要相同,所以除了两端外,其他的都是两两相同的,所以除了两端的颜色外其他的的个数必须为偶数.然后两端的可能相同可能不相同,相同的话,说明所有的都是偶数个数了,不相同的话那就只有这

POJ 2503 Babelfish(字典树)

题目链接:http://poj.org/problem?id=2503 题意:翻译单词,若在词典中找不到则输出eh. 思路:裸的字典树. 代码: #include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> #include <string> #include <vector> using

poj 1204 Word Puzzles(字典树)

题目链接:http://poj.org/problem?id=1204 思路分析:由于题目数据较弱,使用暴力搜索:对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可: 需要注意的是查找时不能匹配了一个单词就不在继续往该方向查找,因为在某个坐标的某个方向上可能会匹配多个单词,所以需要一直 查找直到查找到该方向上最后一个坐标: 代码如下: #include <cstdio> #include <cstring> #include <iostream>

POJ 2408 - Anagram Groups - [字典树]

题目链接:http://poj.org/problem?id=2408 World-renowned Prof. A. N. Agram's current research deals with large anagram groups. He has just found a new application for his theory on the distribution of characters in English language texts. Given such a text

POJ 2418 Hardwood Species(字典树)

Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 20085   Accepted: 7911 Description Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and generally go dormant in the winter. Ameri

poj 2804 词典 (字典树 或者 快排+二分)

2804:词典 总时间限制:  3000ms  内存限制:  65536kB 描述 你旅游到了一个国外的城市.那里的人们说的外国语言你不能理解.不过幸运的是,你有一本词典可以帮助你. 输入 首先输入一个词典,词典中包含不超过100000个词条,每个词条占据一行.每一个词条包括一个英文单词和一个外语单词,两个单词之间用一个空格隔开.而且在词典中不会有某个外语单词出现超过两次.词典之后是一个空行,然后给出一个由外语单词组成的文档,文档不超过100000行,而且每行只包括一个外语单词.输入中出现单词只