题目链接: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的更多相关文章

  1. 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 ...

  2. hdu 1075 (map)

    http://acm.hdu.edu.cn/showproblem.php?pid=1075 What Are You Talking About Time Limit: 10000/5000 MS ...

  3. 字典树 HDU 1075 What Are You Talking About

    http://acm.hdu.edu.cn/showproblem.php?pid=1075 ;}

  4. 题解报告:hdu 1075 What Are You Talking About

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 Problem Description Ignatius is so lucky that he ...

  5. 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 ...

  6. hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

  7. hdu 1075 二分搜索

    还是写一下,二分搜索好了 这道题开数组比较坑... 二分,需要注意边界问题,例如:左闭右闭,左闭右开,否则查找不到or死循环 先上AC代码 #include<iostream> #incl ...

  8. 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 ...

  9. hdu 1075 What Are You Talking About(字典树)

    刚学的字典树,代码写得很不熟练.写法上也没有什么特别的优化,就是以1A为第一目标! 可惜还是失败了. 少考虑了一种情况,就是一个单词是另一个单词前缀的问题,写了好久,还是没有1A.不过感觉对字典树有了 ...

随机推荐

  1. 观察者(observer)设计模式

    转载:http://www.tracefact.net/CSharp-Programming/Delegates-and-Events-in-CSharp.aspx 假设我们有个高档的热水器,我们给它 ...

  2. andoroid项目使用Javah找不到class问题

    比如目录结构是:Soffice\bin\classes\cn\com\isoffice\util\SofficeWebService.class 进入到bin/classes 下使用命令 javah ...

  3. 【转】代码编辑器(二)-SynEdit

    在我去年的时候我就有这个了,而且这是我第二个第三方的控件(第一个是DevExpress),这个是专门做代码编辑器的.安装方法:点我. 安装成功了之后,会在Tool Palette看到两个:SynEdi ...

  4. 第二节:AppDomain

    CLR COM服务器初始化时,会创建一个AppDomain.AppDomain是一组程序集的逻辑容器.CLR初始化时创建的第一个AppDomain称为默认的AppDomain,这个默认的AppDoma ...

  5. 解决在sublime text3在ubuntu下无法输入中文的问题

    方法链接:https://github.com/lyfeyaj/sublime-text-imfix 效果图:

  6. 转 在SQL Server中创建用户角色及授权(使用SQL语句)

     目录 要想成功访问 SQL Server 数据库中的数据 我们需要两个方面的授权 完整的代码示例 使用存储过程来完成用户创建 实例 要想成功访问 SQL Server 数据库中的数据, 我们需要两个 ...

  7. Python数据类型-----数字&字符串

    Python数字类型 int类型表示的范围:-2147483648至2147483648之间,超出这个范围的数字即视为long(长整形) 在Python中不需要事先声明数据类型,它是根据具体的赋值来进 ...

  8. Percona-Xtrabackup 2.3.3 慢查询不再堵塞备份(一)

    在Percona-Xtrabackup 2.3.3[root@b28-19-17 bak]# rpm -q percona-xtrabackuppercona-xtrabackup-2.3.3-1.e ...

  9. Oracle redo 日志切换时间频率

    DB: 11.2.0.3.0 查看Oracle的redo日志切换频率 两条SQL,原理是一样的,第二个用到了统计函数 时间单位:分钟 方法一. select * from v$log a where ...

  10. SQL*PLUS下使用AUTOTRACE、sql顾问、实时sql监控

    高级SQL优化(三) 常用优化工具 ——<12年资深DBA教你Oracle开发与优化—— 高级SQL优化(一) ——<12年资深DBA教你Oracle开发与优化——性能优化部分 1 aut ...