【字典树】hdu 1075 What Are You Talking About
2015-10-30 20:04
393 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1075
查字典的感觉,字典树或者map容器都可以,附2个版本
/*
hdu 1075
方法一:map容器,自带查找O(logn)
方法二:字典树 串的快速检索
注意字符串的读入!
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<algorithm>
#include<sstream>
#define eps 1e-9
#define pi acos(-1)
#define INF 0x7fffffff
#define inf -INF
#define MM 12900
#define N 50
using namespace std;
typedef long long ll;
const int _max = 5e4 + 10;
map<string,string>mp;
string s,a,b;
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
#endif // ONLINE_JUDGE
cin>>s;
while(cin>>a&&a!="END"){
cin>>b;
mp[b] = a;
}
cin>>s;//cout<<s<<endl;
getchar();
a="";
while(getline(cin,s)&&s!="END"){
for(int i = 0; i < s.length();++ i){
if(islower(s[i])){
a+=s[i];
}
else {
if(mp.find(a)!=mp.end()) cout<<mp[a];
else cout<<a;
a="";
printf("%c",s[i]);
}
}
cout<<endl;
}
return 0;
}
查字典的感觉,字典树或者map容器都可以,附2个版本
/*
hdu 1075
方法一:map容器,自带查找O(logn)
方法二:字典树 串的快速检索
注意字符串的读入!
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<algorithm>
#include<sstream>
#define eps 1e-9
#define pi acos(-1)
#define INF 0x7fffffff
#define inf -INF
#define MM 12900
#define N 50
using namespace std;
typedef long long ll;
const int _max = 5e4 + 10;
map<string,string>mp;
string s,a,b;
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
#endif // ONLINE_JUDGE
cin>>s;
while(cin>>a&&a!="END"){
cin>>b;
mp[b] = a;
}
cin>>s;//cout<<s<<endl;
getchar();
a="";
while(getline(cin,s)&&s!="END"){
for(int i = 0; i < s.length();++ i){
if(islower(s[i])){
a+=s[i];
}
else {
if(mp.find(a)!=mp.end()) cout<<mp[a];
else cout<<a;
a="";
printf("%c",s[i]);
}
}
cout<<endl;
}
return 0;
}
#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<vector> #include<queue> #include<set> #include<map> #include<algorithm> #include<sstream> #define eps 1e-9 #define pi acos(-1) #define INF 0x7fffffff #define inf -INF #define MM 12900 #define N 50 using namespace std; typedef long long ll; const int _max = 5e4 + 10; char ans[20]; struct Trie{ Trie *next[26]; int cnt;//从根节点到该节点组成的串的个数 char str[20]; }*root; void insert(char *s,char *a){ Trie *p = root,*pnew; for(int i = 0; i < strlen(s);++ i){ int x = s[i]-'a'; if(p->next[x]==NULL){ pnew = new Trie; pnew->cnt = 0; // pnew->str = NULL; for(int j = 0; j < 26; ++ j) pnew->next[j] = NULL; p->next[x] = pnew; } p=p->next[x]; } p->cnt = 1; strcpy(p->str,a); } int search(char *s){ Trie *p = root; for(int i = 0; i < strlen(s);++ i){ int x = s[i]-'a'; if(p->next[x]==NULL) return 0; p=p->next[x]; } if(p->cnt==1){ strcpy(ans,p->str); return 1; } return 0; } void init(){ root = new Trie; root->cnt = 0; for (int i = 0;i < 26;i ++) root->next[i] = NULL; } int main(){ #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); #endif // ONLINE_JUDGE init(); string s;cin>>s;char a[20],b[20]; while(scanf("%s",a)==1&&strcmp(a,"END")){ scanf("%s",b); insert(b,a); } cin>>s;getchar(); memset(a,0,sizeof(a));int k = 0; while(getline(cin,s)&&s!="END"){ for(int i = 0; i < s.length(); ++ i){ if(islower(s[i])){ a[k++] = s[i]; } else{ a[k] = '\0'; if(search(a)) printf("%s",ans); else printf("%s",a); k = 0; printf("%c",s[i]); } } printf("\n"); } return 0; }
相关文章推荐
- Spark RDD API详解(一) Map和Reduce
- Python中map()函数浅析
- Android使用Google Map浅谈
- Erlang中的映射组Map详细介绍
- 字典树的基本知识及使用C语言的相关实现
- c++中map的基本用法和嵌套用法实例分析
- JavaScript中实现Map的示例代码
- jquery中map函数与each函数的区别实例介绍
- jquery中map函数遍历数组用法实例
- jquery与google map api结合使用 控件,监听器
- jquery遍历数组与筛选数组的方法
- JavaScript中实现map功能代码分享
- 用json方式实现在 js 中建立一个map
- Android Map新用法:MapFragment应用介绍
- js Map List 遍历使用示例
- java实现遍历Map的方法
- velocity显示List与Map的方法详细解析
- Google Map Api和GOOGLE Search Api整合实现代码
- 关于STL中的map容器的一些总结
- Trie树_字典树(字符串排序)简介及实现