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="">题目链接 ...
随机推荐
- NBIbatis web/winform框架
Web框架 调用Bussiness和DataAccess可参考微信框架的后台. Pages/Meeting/MeetingList.aspx Pages/Meeting/MeetingEdit.asp ...
- Windows下使用Redis(一)安装使用
一.Redis 是什么 Redis 是一款依据BSD开源协议发行的高性能Key-Value存储系统(cache and store).它通常被称为数据结构服务器,因为值(value)可以是 字符串(S ...
- Ajax初步理解
最近在项目中经常会使用Ajax技术,用法上倒是熟练了,但是只知其然,不知其所以然,抽时间读了读JavaScript高级程序设计中关于Ajax的介绍有了些初步的理解,在此总结一下. 什么是Ajax Aj ...
- win系统下nodejs安装及环境配置
第一步:下载安装文件下载nodejs,官网:http://nodejs.org/download/,我这里下载的是node-v0.10.28-x86.msi,如下图: 第二步:安装nodejs下载完成 ...
- 数据类型/强制类型转换 和运算符---标识符规则/关键字 a++和++a区别
3.2关键字都是小写,TRUE FALSE NULL都不是Java关键字 3.3数据类型 变量相当于一个有名称的容器,该容器用于装各种不同类型的数据 Java类型分为2种 基本类型: 引用类型: 基本 ...
- android开源项目和框架
特效: http://www.androidviews.net/ http://www.theultimateandroidlibrary.com/ 常用效果: 1. https://github.c ...
- 题目一:一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米)?
题目一:一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米)? //一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米 doub ...
- 详解Bootstrap进度条组件
在网页中,进度条的效果并不少见,如:平分系统.加载状态等,进度条组件使用了css3的transition和animation属性来完成一些特效,这些特效在IE9及IE9以下版本.Firefox的老版本 ...
- 【VerySky原创】后台JOB运行-相关表
[VerySky原创] TBTCP 批作业步骤概述TBTCO 作业状态概述表TBTCS 批计划表TBTC_SPOOLID Background Processing Spool ...
- linux下查看磁盘空间 [转]
如果要查看磁盘还剩多少空间,当然是用df的命令了. [root@localhost ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/sda2 ...