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.不过感觉对字典树有了 ...
随机推荐
- WIN8 下 Hyper-V和Vmware Workstation
1 管理员身份运行命令提示符 cmd bcdedit /copy {current} /d “Windows Without Hyper-V 2 记下 { } 中的代码 bcdedit /set {X ...
- angularJS 数组更新时重新排序之解决方案一:这个坑,绕开吧,不跳了……
今天产品大人发现了一bug,图表数据和数据库总是对不上,原因是当前端更新数组时,angularJS默认对数组进行了排序. // 点击事件:input复选框 $scope.fnClickUpdateAr ...
- c语言学习的第五天
#include<stdio.h> #include<stdbool.h> int main() { _Bool num=1; if (num==true); { printf ...
- MongoDB简述
简介 MongoDB is an open-source document database that provides high performance, high availability, an ...
- 更强的微光闪烁效果--第三方开源--Shimmer-android
Shimmer-android在github上的项目主页是:https://github.com/RomainPiel/Shimmer-android Shimmer-android干脆在Androi ...
- 共享内存shared pool (5):详解一条SQL在library cache中解析
前面介绍的 shared pool,library cache结构,都是为了说明一条SQL是如何被解析的.先看下面的图: 图中涉及的各结构简单介绍 父HANDLE,里面有父游标堆0的地址.. 父游标堆 ...
- 7.python字符串-内置方法分析
上篇对python中的字符串内置方法进行了列举和简单说明,但这些方法太多,逐一背下效率实在太低,下面我来对这些方法按照其功能进行总结: 1.字母大小写相关(中文无效) 1.1 S.upper() -& ...
- [terry笔记]RMAN综合学习之配置
[terry笔记]RMAN综合学习之备份http://www.cnblogs.com/kkterry/p/3308405.html [terry笔记]RMAN综合学习之恢复 http://www.cn ...
- unix的策略与机制
策略同机制分离,接口同引擎分离 Linux/Unix设计理念提供的一种机制不是策略.如果说机制是一种框架,那么,策略就是填充框架的一个个具体实施.机制提供的就是一种开放而宽松的环境,而策略就是在这个环 ...
- poj 2485 Highways
题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...