POJ 1816 Wild Words
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 4412 | Accepted: 1149 |
Description
There are many word patterns and some words in your hand. For each word, your task is to tell which patterns match it.
Input
You can assume that the length of patterns will not exceed 6, and the length of words will not exceed 20.
Output
Sample Input
5 4
t*
?h*s
??e*
*s
?*e
this
the
an
is
Sample Output
0 1 3
0 2 4
Not match
3
题目大意:输入N,M然后输入N行字符串,由'?','*'和26个小写字母组成,'?'代表任意一个小写字母,'*'代表0个或者多个小写字母,紧接着输入M行字符串,问每行字符串和前面N行字符串中哪些是等价的。
解题方法:经典的DFS+字典树,先建立建立一颗字典树,然后用DFS进行搜索。
#include <stdio.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std; int ans[];
int nCount;
char str[]; typedef struct node
{
vector <int> id;
node *next[];
node()
{
id.clear();
memset(next, , sizeof(next));
}
~node()
{
id.clear();
}
}TreeNode; int GetIndex(char ch)
{
switch(ch)
{
case '?':
return ;
case '*':
return ;
default:
return ch - 'a';
}
} //建立字典树
void Insert(TreeNode *pRoot, char pstr[], int id)
{
int nLen = strlen(pstr);
TreeNode *p = pRoot;
for (int i = ; i < nLen; i++)
{
int index = GetIndex(pstr[i]);
if (p->next[index] == NULL)
{
p->next[index] = new TreeNode;
}
p = p->next[index];
}
p->id.push_back(id);//每个单词的结尾保存该单词的编号
} void DFS(TreeNode *pRoot, int index)
{
if (pRoot->next[] != NULL)
{
//忽略掉'*',即'*'代表0个字母
DFS(pRoot->next[], index);
}
if (index == strlen(str))
{
//如果遍历到了最后一个字母,则把编号加进去
for (int i = ; i < pRoot->id.size(); i++)
{
ans[nCount++] = pRoot->id[i];
}
return;
}
//如果字典树和字符串当前都是字母,则同时跳过该字母
if (pRoot->next[GetIndex(str[index])] != NULL)
{
DFS(pRoot->next[GetIndex(str[index])], index + );
}
if (pRoot->next[] != NULL)
{
//如果字典树中当前是'?',直接跳过
DFS(pRoot->next[], index + );
}
if (pRoot->next[] != NULL)
{
//如果字典树中当前是‘*’,则让‘*’代表多个字符
for (int i = index; i < strlen(str); i++)
{
DFS(pRoot->next[], i + );
}
}
} void DeleteNode(TreeNode *pRoot)
{
if (pRoot != NULL)
{
for (int i = ; i < ; i++)
{
DeleteNode(pRoot->next[i]);
}
}
delete pRoot;
} int main()
{
int N, M, nID = ;
scanf("%d%d", &N, &M);
TreeNode *pRoot = new TreeNode;
for (int i = ; i < N; i++)
{
scanf("%s", str);
Insert(pRoot, str, nID++);
}
for (int i = ; i < M; i++)
{
nCount = ;
scanf("%s", str);
DFS(pRoot, );
if (nCount == )
{
printf("Not match\n");
}
else
{
sort(ans, ans + nCount);
printf("%d", ans[]);
for (int j = ; j < nCount; j++)
{
if (ans[j - ] != ans[j])
{
printf(" %d", ans[j]);
}
}
printf("\n");
}
}
DeleteNode(pRoot);
return ;
}
POJ 1816 Wild Words的更多相关文章
- POJ 1816 - Wild Words - [字典树+DFS]
题目链接: http://poj.org/problem?id=1816 http://bailian.openjudge.cn/practice/1816?lang=en_US Time Limit ...
- 【POJ】1816 Wild Words
DFS+字典树.题目数据很BT.注意控制DFS深度小于等于len.当'\0'时,还需判断末尾*.另外,当遇到*时,注意讨论情况. #include <iostream> #include ...
- poj 1816 (Trie + dfs)
题目链接:http://poj.org/problem?id=1816 思路:建好一颗Trie树,由于给定的模式串可能会重复,在原来定义的结构体中需要增加一个vector用来记录那些以该节点为结尾的字 ...
- POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)
本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...
- [转] POJ字符串分类
POJ 1002 - 487-3279(基础)http://acm.pku.edu.cn/JudgeOnline/problem?id=1002题意:略解法:二叉查找数,map,快排... POJ 1 ...
- E - Petya and Exam CodeForces - 832B 字典树+搜索
E - Petya and Exam CodeForces - 832B 这个题目其实可以不用字典树写,但是因为之前写过poj的一个题目,意思和这个差不多,所以就用字典树写了一遍. 代码还是很好理解的 ...
- 字典树trie的学习与练习题
博客详解: http://www.cnblogs.com/huangxincheng/archive/2012/11/25/2788268.html http://eriol.iteye.com/bl ...
- POJ 3340 & HDU 2410 Barbara Bennett's Wild Numbers(数学)
题目链接: PKU:http://poj.org/problem?id=3340 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2410 Descript ...
- HDU 1816, POJ 2723 Get Luffy Out(2-sat)
HDU 1816, POJ 2723 Get Luffy Out pid=1816" target="_blank" style="">题目链接 ...
随机推荐
- [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)
Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...
- [ucgui] 彩色条函数
/* 颜色条 */ void ShowColorBar(void) { , y0 = , yStep = , i; int NumColors = LCD_GetDevCap(LCD_DEVCAP_N ...
- 找出字符串中第一个不重复的字符(JavaScript实现)
如题~ 此算法仅供参考,小菜基本不懂高深的算法,只能用最朴实的思想去表达. //找出字符串中第一个不重复的字符 // firstUniqueChar("vdctdvc"); --& ...
- Unity3d碰撞检测中碰撞器与触发器的区别
要产生碰撞必须为游戏对象添加刚体(Rigidbody)和碰撞器,刚体可以让物体在物理影响下运动.碰撞体是物理组件的一类,它要与刚体一起添加到游戏对象上才能触发碰撞.如果两个刚体相互撞在一起,除非两个对 ...
- python星号变量
python 元组 tupletup1 = ('physics', 'chemistry', 1998, 2000)tup2 = (1, 2, 3, 4, 5)tup3 = 'a', 'b', 'c' ...
- How Tomcat works — 五、tomcat启动(4)
前面摆了三节的姿势,现在终于要看到最终tomcat监听端口,接收请求了. 目录 Connector Http11Protocol JIoEndpoint 总结 在前面的初始化都完成之后,进行Conne ...
- Nginx缓存、压缩配置
1.缓存配置 只需在http的server模块里配置即可,如: location ~.*\.(jpg|png|gif)$ { expires 30d; } location ~.*\.(css|js) ...
- C++Builder RAD Studio XE, UTF-8 String 转换为 char * 字符串的最简单方式, 常用于sqlite3开发
前段时间突然使用sqlite3开发,中间需要用中文,XE的缺省char*直接使用中文,在sqlite *.db3的数据库表格中显示是乱码,用数据库管理器来浏览等管理时非常不便. 于是决定还是使用utf ...
- zz 游戏程序员的学习之路(中文版)
游戏程序员的学习之路(中文版) Milo Yip · 1 天前 感谢 @楚天阔(tkchu)编写脚本及整理中文译本数据,自动从英文版生成中文版,SVG / PDF 版本中的书籍图片现在链接至豆瓣页面. ...
- 了解 JavaScript (6)– 广告条(Banner)
在 Web 上冲浪时,常常会见到定期在图像之间切换的广告条.我们可以用 JavaScript 来实现,重复循环显示它们. 创建循环的广告条 RotatingBanner.html 页面中在循环的广告条 ...