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

C++版本一

题解:字典树||map

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=500000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q;
int ans,cnt,flag,temp;
int a[N][26];
char str[N],rec[N],str1[N],str2[N];
map<string,string>p;
bool en[N];
char cp[N][20];
void insert(char *s,char *s2){int len=strlen(s),p=0;for(int i=0;i<len;i++){int ch=s[i]-'a';if(a[p][ch]==0)a[p][ch]=++cnt;p=a[p][ch];}en[p]=1;strcpy(cp[p],s2);
}
void search(char* s){int len=strlen(s),p=0;for(int i=0;i<len;i++){int ch=s[i]-'a';if(a[p][ch]==0){printf("%s",s);return;}p=a[p][ch];}if(en[p])printf("%s",cp[p]);elseprintf("%s",s);}int main()
{
#ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout);
#endif//scanf("%d",&n);//scanf("%d",&t);//while(t--){}scanf("%s",str);while(scanf("%s",str)){if(strcmp(str,"END")==0)break;scanf("%s",str2);insert(str2,str);}scanf("%s",str);getchar();while (gets(str) && strcmp(str, "END")) {int len = strlen(str);int idx = 0;for (int i=0;i<=len - 1;i++) {if (islower(str[i])) {rec[idx++] = str[i];} else {rec[idx] = 0;search(rec);idx = 0;printf("%c", str[i]);}}puts("");}//cout << "Hello world!" << endl;return 0;
}

C++版本二

题解:

可以用map赤裸裸地做,但是比较花费时间,虽然这题时间给了5s,map解法是能过的。 
不过Trie解法500+ms,果然Trie字典树才是正解啊。 
Trie入门题。

另外发现ios_base::sync_with_stdio(0)这句话是关闭IO同步的,如果把cin和gets混用就不同步...坑了好久..

#include <cstdio>
#include <cstring>
#include <cctype>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;string a, b;
char s[3010];
map<string, string> mp;int main() {// ios_base::sync_with_stdio(0);cin >> a;while (cin >> a) {if (a == "END")break;cin >> b;mp[b] = a;}cin >> a;getchar();while (1) {gets(s);if (!strcmp(s, "END"))break;int len = strlen(s);a = "";repf (i, 0, len - 1) {if (islower(s[i])) {a += s[i];} else {if (mp.find(a) != mp.end())cout << mp[a];elsecout << a;a = "";cout << s[i];}}cout << endl;}return 0;
}

C++版本三

#include <cstdio>
#include <cstring>
#include <cctype>
#include <iostream>
#include <algorithm>
using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll;const int N = 3010;
const int SIZE = 26;// pointer trie
struct Node {char* val;Node *next[SIZE];
};struct PTrie {Node *root;PTrie() { root = newNode(); }void init() { del(root); root = newNode(); }inline int idx(char c) { return c - 'a'; }Node *newNode() {Node *u = new Node;repf (i, 0, SIZE - 1) {u->next[i] = NULL;}u->val = NULL;return u;}void insert(char *s, char *v) {Node *u = root;int len = strlen(s);repf (i, 0, len - 1) {int c = idx(s[i]);if (u->next[c] == NULL)u->next[c] = newNode();u = u->next[c];}u->val = new char[11];strcpy(u->val, v);}void find(char *s) {Node*u = root;int len = strlen(s);repf (i, 0, len - 1) {int c = idx(s[i]);if (u->next[c] == NULL) {printf("%s", s);return;}u = u->next[c];}if (u->val)printf("%s", u->val);elseprintf("%s", s);}void del(Node *rt) {if (rt == NULL)return;else {repf (i, 0, SIZE - 1)if (rt->next[i])del(rt->next[i]);}delete rt->val;delete rt;}
} trie;char a[11], b[11];
char str[N], rec[N];int main() {// ios_base::sync_with_stdio(0);scanf("%s", a);while (scanf("%s %s\n", a, b) && strcmp(a, "END")) {//cout << a << b << endl;trie.insert(b, a);}while (gets(str) && strcmp(str, "END")) {int len = strlen(str);int idx = 0;repf (i, 0, len - 1) {if (islower(str[i])) {rec[idx++] = str[i];} else {rec[idx] = 0;trie.find(rec);idx = 0;printf("%c", str[i]);}}puts("");}
}

最新文章

  1. 微软每年豪砸安全研发 10 亿美元,聊聊背后的技术密码
  2. backbone.js全栈开发
  3. 同居1月 VS 同居1年,太真实了...
  4. python数据分析图表展示_1行代码实现Python数据分析:图表美观清晰,自带对比功能丨开源...
  5. 别说我懂社交网络: 关于社交网络分析的一头雾水
  6. Spring Boot的学习之路(02):和你一起阅读Spring Boot官网
  7. apache的源码包编译
  8. 基于Snap软件将2A哨兵数据转换为ENVI格式
  9. freeswitch 使用ipset和iptables阻止国外ip扫描端口
  10. 利用Python下载并解压压缩文件
  11. Symbol数据类型
  12. 计算机网络拓扑图 模板,网络拓扑图绘制.doc
  13. Acquiring a token for Kubernetes dashboard
  14. 手绘topo图组件 vue + element-ui + jtopo
  15. 计算机图形学四:着色-Shading
  16. Pr:导出设置之元数据
  17. C++沙海拾遗(三)
  18. mysql中locat函数,MySQL中的LOCATE和POSITION函数使用方法
  19. android10手机运行内存怎么查看,安卓手机怎么查看手机内存
  20. No mapping found for HTTP request with URI问题解决

热门文章

  1. inrange函数_Python 初学者必备的常用内置函数
  2. sql查询两个表结果相减_SQL 操作结果集 -并集、差集、交集、结果集排序
  3. c语言坐标三角形判断,【C语言】判断三角形类型(示例代码)
  4. 七十七、 二叉树的层次遍历和最大深度
  5. 九、给小白看的第二篇Python基础教程
  6. Transformer升级之路:二维位置的旋转式位置编码
  7. 清华大学王晨阳:轻量级Top-K推荐框架及相关论文介绍
  8. 直播 | 华为云卢栋才:对话机器人的应用实践和最新进展
  9. 上有硬核理论下能操刀AI落地,这里走出来的人都有“开挂人生”
  10. 刚刚,阿里开源了一系列重磅技术炸弹!| 程序员必看