POJ 1816 Trie

链接:

http://poj.org/problem?id=1816

题意:

先给你n个字符串,这些字符串包含小写字母,‘?’和‘*’,其中

‘?’可以表示任意一个字符,‘*’可以表示任意长度的任意字符,包括0个

然后m次询问,每次给你一个字符串,问你它和哪些字符串匹配

题解:

5月第一发AC真不容易啊(其实上个月就该A的

这道题思路很简单,就是先建一个字典树,然后在字典树上跑dfs就好了,但是有几个需要注意的地方

当然如果方法不一样,可能就不会遇到这么多恶心的问题

首先最坑的就是题目中给的字符串会有一样的,所以就必须要用vector来存了

开始的时候dfs写错了,多写了一层循环,tle了

然后改完之后,wa了,发现是没有判断最后还有一个*的情况,然后加上之后又wa了

然后发现结尾可能不只有一个*,我的判断方式有问题,改好之后又wa了

忍不了就看了discuss,原来可能有重复的字符串,改成vector后结果又mle了。。。

果断把一个int数组改成short,然后又超时了。

大概是没加ios::sync_with_stdio(false), cin.tie(0);吧,加上之后继续wa。。

然后一想有重复的结点的话,直接输出是不行的(因为怕mle所以没有把答案先存下来)

虽然题目上没有说要排序输出,但既然不是special judge,还是要排序的,所以就只好先存了下来

然后排序,去重(我也不知道我为啥要去重,顺手去了0.0) 然后终于过了!!!

代码:

int pi = 1;

struct Node {
	short next[30];
	VI id;
	bool end;
}tree[MAXN * 6];

void insert(string s, int id) {
	int index, p, i;
	for (i = p = 0; s[i]; i++) {
		if (isalpha(s[i])) index = s[i] - ‘a‘;
		else if (s[i] == ‘?‘) index = 26;
		else index = 27;
		if (tree[p].next[index] == 0)
			tree[p].next[index] = pi++;
		p = tree[p].next[index];
	}
	tree[p].id.pb(id);
	tree[p].end = 1;
}

VI ans;

void dfs(string s, int cnt, int p) {
	if (cnt == s.length()) {
		if (tree[p].end)
			rep(i, 0, tree[p].id.size())
			ans.insert(ans.end(), all(tree[p].id));
		if (!tree[p].next[27]) return;
	}
	int index = s[cnt] - ‘a‘;
	if (tree[p].next[index])
		dfs(s, cnt + 1, tree[p].next[index]);
	if (tree[p].next[26])
		dfs(s, cnt + 1, tree[p].next[26]);
	if (tree[p].next[27]) rep(j, cnt, s.size() + 1)
		dfs(s, j, tree[p].next[27]);
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	int n, m;
	cin >> n >> m;
	rep(i, 0, n) {
		string s;
		cin >> s;
		insert(s, i);
	}
	while (m--) {
		ans.clear();
		string s;
		cin >> s;
		dfs(s, 0, 0);
		sort(all(ans));
		ans.erase(unique(all(ans)), ans.end());
		if (ans.empty()) cout << "Not match";
		else rep(i, 0, ans.size()) cout << ans[i] << ‘ ‘;
		cout << endl;
	}
	return 0;
}                                    
时间: 2024-08-14 21:33:15

POJ 1816 Trie的相关文章

poj 1056 Trie树判断哈夫曼编码是否合法

理解了Trie树然后就能1A   其实估计这个题随便做做就能A掉,可能不需要高级数据. 先贴吉林大学的代码模板 /*==================================================*| Trie树(k叉) | INIT: init(); | 注: tree[i][tk]>0时表示单词存在, 当然也可赋予它更多含义; \*==================================================*/ const int tk = 26,

POJ 2503 Trie

链接: http://poj.org/problem?id=2503 题意: 给定一些字符串以及它在外星语言中的对应翻译,现在有若外星语言中的串,要把它们翻译成英语 题解: 这道题map,hash,trie都是可以做的 然而我用g++提交发现map和trie都超时了,换成c++就全都过了 map用了1141ms,trie只要485ms 代码: 31 vector<string> word; 32 int pi =1; 33 34 struct Node { 35 int next[26]; 3

poj 2945 trie树统计字符串出现次数

用记录附加信息的val数组记录次数即可. PS:trie树还有种动态写法,使用指针和动态分配内存代替了连续的ch数组,更加节省内存. Reference:http://blog.csdn.net/architect19/article/details/8966247 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 #define maxnode

POJ 3630 Trie

链接: http://poj.org/problem?id=3630 题意: 给你n个字符串,判断有没有字符串是其他字符串的前缀 题解: 建一个字典树,在插入的过程中,如果没有新建一个结点,那这个字符串肯定是其他字符串的前缀, 如果新建结点的时候发现,有的字符串以这个字符结尾,那肯定有字符串是这个字符串的前缀 代码: 31 int pi = 1; 32 int fg; 33 34 struct Node { 35 int next[10]; 36 bool end; 37 }tree[MAXN]

poj 2503 Trie树

典型的Trie树, 算是复习一下字符串吧, 就是输入有点恶心,代码如下: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 500000+100; struct Trie{ bool isword; int next[26]; char words[15]; Trie(){ memset(next, -1, sizeof(next

Poj 2001 (Trie 前缀树)

#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<cmath> #include<vector> #include<queue> #include<map> #define MAXN 400010 #defi

poj 2001 trie

第一道trie 还需要写题来建立自己的代码习惯. 1 #include <cstdio> 2 #include <vector> 3 #include <algorithm> 4 #define maxn 20010 5 using namespace std; 6 7 struct node { 8 char v; 9 int sz; 10 bool isword, mark; 11 node *son[26], *pre; 12 }pool[maxn], *tail

[转] POJ字符串分类

POJ 1002 - 487-3279(基础)http://acm.pku.edu.cn/JudgeOnline/problem?id=1002题意:略解法:二叉查找数,map,快排... POJ 1200 - Crazy Search(基础)http://acm.pku.edu.cn/JudgeOnline/problem?id=1200题意:找出不相同的子串数量,字母表大小和子串长度会给定,这题很推荐hash入门者一做解法:hash(建议karp-rabin) POJ 1204 - Word

字典树trie的学习与练习题

博客详解: http://www.cnblogs.com/huangxincheng/archive/2012/11/25/2788268.html http://eriol.iteye.com/blog/1166118 http://www.360doc.com/content/12/1116/15/9615799_248213540.shtml http://www.cnblogs.com/tanky_woo/archive/2010/09/24/1833717.html http://bl