hdu 1075 What Are You Talking About
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075
题意:比较简单,易懂,这里不做说明。
解法:第一种方法:用map映射,耗时1000+ms;第二种方法:用字典树处理,500+ms。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#define inf 0x7fffffff
using namespace std;
char s1[],s2[];
typedef struct NODE
{
NODE *child[];
char str[];
int ok;
NODE () {ok= ;str[]= ;for (int i= ;i< ;i++) child[i]=NULL; } }node;
void insert(node *root)
{
node *cur=root;
int len=strlen(s2);
int k;
for (int i= ;i<len ;i++)
{
k=s2[i]-'a';
if (cur->child[k]!=NULL)
{
cur=cur->child[k];
}else
{
node *q=new node;
q->ok=;
q->str[]=;
for (int i= ;i< ;i++) q->child[i]=NULL;
cur->child[k]=q;
cur=cur->child[k];
}
}
cur->ok=;
strcpy(cur->str,s1);
}
int findTree(node *root)
{
node *cur=root;
int len=strlen(s2);
for (int i= ;i<len ;i++)
{
int k=s2[i]-'a';
cur=cur->child[k];
if (cur==NULL) return ;
}
if (cur->ok == ) {printf("%s",cur->str);return ;}
return ;
}
void del(node *root)
{
for (int i= ;i< ;i++) if (root->child[i]) del(root->child[i]);
delete root;
root=NULL;
}
int main()
{
memset(s1,,sizeof(s1));
memset(s2,,sizeof(s2));
node *root=new node;
cin>>s1;
while (scanf("%s",s1)!=EOF)
{
if (strcmp(s1,"END")==) break;
scanf("%s",s2);
//cout<<s1<<" "<<s2<<endl;
insert(root);
}
cin>>s1;
char s[];
memset(s,,sizeof(s));
getchar();
while (gets(s))
{
//if (s[0]=='E' && s[1]=='N' && s[2]=='D') break;
if (strcmp(s,"END")==) break;
memset(s2,,sizeof(s2));
int cnt=;
int flag=;
int len=strlen(s);
for (int i= ;i<len ;i++)
{
if (s[i]>='a' && s[i]<='z')
{
flag=;
s2[cnt++]=s[i];
}
else if (flag)
{
flag=;
s2[cnt]=;
int m=findTree(root);
if (!m) printf("%s",s2);
cnt=;
memset(s2,,sizeof(s2));
}
if (!flag) printf("%c",s[i]);
}
if (flag)
{
s2[cnt]=;
int m=findTree(root);
if (!m) printf("%s",s2);
}
cout<<endl;
}
del(root);
//system("pause");
return ;
}
hdu 1075 What Are You Talking About的更多相关文章
- HDU 1075 What Are You Talking About(Trie的应用)
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
- hdu 1075 (map)
http://acm.hdu.edu.cn/showproblem.php?pid=1075 What Are You Talking About Time Limit: 10000/5000 MS ...
- 字典树 HDU 1075 What Are You Talking About
http://acm.hdu.edu.cn/showproblem.php?pid=1075 ;}
- 题解报告:hdu 1075 What Are You Talking About
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 Problem Description Ignatius is so lucky that he ...
- HDU 1075 What Are You Talking About (Trie)
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
- hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
- hdu 1075 二分搜索
还是写一下,二分搜索好了 这道题开数组比较坑... 二分,需要注意边界问题,例如:左闭右闭,左闭右开,否则查找不到or死循环 先上AC代码 #include<iostream> #incl ...
- HDOJ/HDU 1075 What Are You Talking About(字符串查找翻译~Map)
Problem Description Ignatius is so lucky that he met a Martian yesterday. But he didn't know the lan ...
- hdu 1075 What Are You Talking About(字典树)
刚学的字典树,代码写得很不熟练.写法上也没有什么特别的优化,就是以1A为第一目标! 可惜还是失败了. 少考虑了一种情况,就是一个单词是另一个单词前缀的问题,写了好久,还是没有1A.不过感觉对字典树有了 ...
随机推荐
- Entity Framework中的多个库操作批量提交、事务处理
在Entity Framework 中使用SaveChanges()是很频繁的,单次修改或删除数据后调用SaveChanges()返回影响记录数. 要使用批量修改或者批量删除数据,就需要SaveCha ...
- js各种宽高(2)
在javascript和jquery中,都有对各种高度的写法,在这里,我们就着重讲一下窗口.文档等高度的理解.(宽度和高度差不多!) jquery的各种高度 首先来说一说$(document)和$(w ...
- Redis源码研究--字典
计划每天花1小时学习Redis 源码.在博客上做个记录. --------6月18日----------- redis的字典dict主要涉及几个数据结构, dictEntry:具体的k-v链表结点 d ...
- slidingmenu
slidingmenu是一个开源组件.提供了左滑菜单和右滑菜单.下面是一个使用例子的工程源码. 在项目中不去管源码仅仅只是使用的话,主要需要实现的有3个类 MainActivity:这 ...
- C#高级功能(四)扩展方法和索引
扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用.扩展方法被定义为静态方法,但 ...
- 【转】Linux Soclet编程
原文地址:http://www.cnblogs.com/skynet/archive/2010/12/12/1903949.html “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编 ...
- 史上最简单的个人移动APP开发入门--jQuery Mobile版跨平台APP开发
书是人类进步的阶梯. ——高尔基 习大大要求新新人类要有中国梦,鼓励大学生们一毕业就创业.那最好的创业途径是什么呢?就是APP.<构建跨平台APP-jQuery Mobile移动应用实战> ...
- 2.html5的基本格式
分享完html语言的核心之后,是时候开始写了.理论上,只要符合格式要求,就算是用记事本也可以写.但是,这种蛋疼且生产力低下的行为还是少做的好,选一个适合自己的IDE才是上上之选,至于哪个合适自己,那就 ...
- ado.net的5个主要对象
connection 连接对象 command 命令对象,指示要执行的命令和存储过程! datareader是一个向前的只读的数据流. dataadapter是功能强大的适陪器,支持增删改查的功能 d ...
- 浅谈Objective—C中的面向对象特性
Objective-C世界中的面向对象程序设计 面向对象称程序设计可能是现在最常用的程序设计模式.如何开发实际的程序是存在两个派系的-- 面向对象语言--在过去的几十年中,很多的面向对象语言被发明出来 ...