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.不过感觉对字典树有了 ...
随机推荐
- 通过 XML HTTP 把文本文件载入 HTML 元素
新建一个.aspx文件 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="01-通 ...
- redis 配置文件解读
# Redis 配置文件 # 当配置中需要配置内存大小时,可以使用 1k, 5GB, 4M 等类似的格式,其转换方式如下(不区分大小写) # # 1k => 1000 bytes # 1kb = ...
- Linux服务器的初步配置流程
作者: 阮一峰 日期: 2014年3月14日 开发网站的时候,常常需要自己配置Linux服务器. 本文记录配置Linux服务器的初步流程,也就是系统安装完成后,下一步要做的事情.这主要是我自己的总结和 ...
- Java实现九九乘法表的输出
九九乘法表一般为三角形,每个数分别和从1到自身的数相乘然后把结果列出来,即要用到两层循环,外层是从1到9for(i=1;i<=9;i++),内层是当前数和从1到自身相乘for(j=1;j< ...
- DevExpress GridControl 使用方法技巧 总结 收录整理
一.如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 二.如何新增一条记录 ().gridView.AddNe ...
- Python-Day12 Python mysql and ORM
一.Mysql数据库 1.什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库, 每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据 ...
- python 面向对象、特殊方法与多范式、对象的属性及与其他语言的差异
1.python 面向对象 文章内容摘自:http://www.cnblogs.com/vamei/archive/2012/06/02/2532018.html 1.__init__() 创建对 ...
- 算法系列8《Base64》
Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,Base64编码可用于在HTTP环境下传递较长的标识信息.在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单 ...
- 九度oj 1407 快速找出最小数
原题链接:http://ac.jobdu.com/problem.php?pid=1407 线段树,区间更新,查询区间最小值. 注意区间更新,查询的时候,区间$\begin{align*}[L,R] ...
- 2015 年开源前端框架盘点 TOP 20
1.名称:Bootstrap 类别/语言:HTML.CSS.JavaScript 创建者: Twitter 人气:在Github上有91007 stars 描述:主流框架中毋庸置疑的老大,Bootst ...