题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 ,字典树的字符串映射。

  


  题意是给你每个火星文单词对应的英语,然后让你把一篇火星文文章给翻译成英语。

解法:

  在Trie树的每个结束标志处加一个字符串,这样就可以对每个火星文单词构造映射。构造映射后就可以处理翻译部分,可以用gets读入一行,然后对这一行进行处理,注意标点符号的情况。最后还有注意数组开大点。

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL __int64
const int maxn = + ;
const int sigma_size = ;
struct Trie {
int ch[maxn][sigma_size];
char str[maxn][];
bool isEnd[maxn];
int size;
void init() {
size = ;
memset(ch[] , , sizeof(ch[]));
memset(isEnd , , sizeof(isEnd));
}
int index(char c) { return c - 'a'; }
void insert(char *s , char *s0) {
int i , rt;
for(i = rt = ; s[i] != '\0' ; i++) {
int c = index(s[i]);
if(!ch[rt][c]) {
memset(ch[size] , , sizeof(ch[size]));
ch[rt][c] = size++;
}
rt = ch[rt][c];
}
strcpy(str[rt] , s0);
isEnd[rt] = ;
}
char *find(char *s) {
int i , rt;
for(i = rt = ; s[i] != '\0' ; i++) {
int c = index(s[i]);
if(!ch[rt][c])
return "";
rt = ch[rt][c];
}
return (isEnd[rt]) ? str[rt] : "";
}
} trie;
char s1[maxn] , s2[maxn];
int main()
{
trie.init();
while(~scanf("%s" , s1)) {
if(!strcmp(s1 , "START")) continue;
if(!strcmp(s1 , "END")) break;
scanf("%s" , s2);
trie.insert(s2 , s1);
}
getchar();
while(gets(s1)) {
if(!strcmp(s1 , "START")) continue;
if(!strcmp(s1 , "END")) break;
int i = ;
while(s1[i] != '\0') {
if(s1[i] >= 'a' && s1[i] <= 'z') {
int j = ;
while(s1[i] != '\0' && s1[i] >= 'a' && s1[i] <= 'z') {
s2[j++] = s1[i++];
}
s2[j] = '\0';
char *tmp = trie.find(s2);
if(tmp[] == '') printf("%s" , s2);
else printf("%s" , tmp);
} else {
putchar(s1[i++]);
}
}
puts("");
}
return ;
}

HDU1075 字典树 + 字符串映射的更多相关文章

  1. I: Carryon的字符串排序(字典树/map映射)

    2297: Carryon的字符串 Time Limit: C/C++ 1 s      Java/Python 3 s      Memory Limit: 128 MB      Accepted ...

  2. HDU1075 字典树板子题

    题意 :给出两组字符串 一一映射,给出一种组成的文字,要求映射成另外一种思路:使用字典树,把映射的另外一个字符存在字典树的单词节点处  例如 abc   123 则把123存在abc节点中的c处即可 ...

  3. POJ 1002 487-3279(字典树/map映射)

    487-3279 Time Limit: 2000MS        Memory Limit: 65536K Total Submissions: 309257        Accepted: 5 ...

  4. Trie树|字典树(字符串排序)

    有时,我们会碰到对字符串的排序,若采用一些经典的排序算法,则时间复杂度一般为O(n*lgn),但若采用Trie树,则时间复杂度仅为O(n). Trie树又名字典树,从字面意思即可理解,这种树的结构像英 ...

  5. 算法学习笔记(一)C++排序函数、映射技巧与字典树

    1.头文件algorithm中有函数sort()用于排序,参数为:排序起始地址,排序结束地址,排序规则(返回bool型)例如,要将array[] = {5,7,1,2,9}升序排列,则使用: bool ...

  6. 字符串hash与字典树

    title: 字符串hash与字典树 date: 2018-08-01 22:05:29 tags: acm 算法 字符串 概述 这篇主要是关于字符串里的 字符串hash 和 字符串字典树,,两个都是 ...

  7. 【字符串算法】字典树(Trie树)

    什么是字典树 基本概念 字典树,又称为单词查找树或Tire树,是一种树形结构,它是一种哈希树的变种,用于存储字符串及其相关信息. 基本性质 1.根节点不包含字符,除根节点外的每一个子节点都包含一个字符 ...

  8. HDU 1671 Phone List (qsort字符串排序与strncmp的使用 /字典树)

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. 2017ACM暑期多校联合训练 - Team 6 1001 HDU 6096 String (字符串处理 字典树)

    题目链接 Problem Description Bob has a dictionary with N words in it. Now there is a list of words in wh ...

随机推荐

  1. CentOS6.5添加rbd模块

    [root@ceph-monitor opt]# modprobe rbd   FATAL: Module rbd not found. Once you have deployed the almi ...

  2. P2939 [USACO09FEB]改造路Revamping Trails(分层图最短路)

    传送门 完了我好像连分层图最短路都不会了……果然还是太菜了…… 具体来说就是记录一个步数表示免费了几条边,在dijkstra的时候以步数为第一关键字,距离为第二关键字.枚举边的时候分别枚举免不免费下一 ...

  3. 日志记录:MySQL系列之十一

    一.SQL命令历史 ~/.mysql_history 记录了在mysql中执行的命令历史 二.事务日志 transaction log:事务型存储引擎自行管理和使用 在一个事务提交后还没有存到磁盘的情 ...

  4. iOS ksyhttpcache音视频缓存

    pod 'ksyhttpcache' 桥接文件 引入 #import <KSYHTTPCache/KSYHTTPProxyService.h> 带appdelegate里初始化 KSYHT ...

  5. git教程1-gitlab部署

    https://about.gitlab.com/install/#centos-7 https://mirror.tuna.tsinghua.edu.cn/help/gitlab-ce/ gitla ...

  6. iptables端口转发规则(内网端口转外网端口)

    需求:外网124.202.173.118需要访问 10.45.225.70的内网54032端口,10.45.225.70服务器有公网地址139.129.109.81将内网地址端口转发到外网地址端口,并 ...

  7. python大战机器学习——半监督学习

    半监督学习:综合利用有类标的数据和没有类标的数据,来生成合适的分类函数.它是一类可以自动地利用未标记的数据来提升学习性能的算法 1.生成式半监督学习 优点:方法简单,容易实现.通常在有标记数据极少时, ...

  8. spring定时任务的集中实现

    转载博主:感谢博主 http://gong1208.iteye.com/blog/1773177 Spring定时任务的几种实现 近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前 ...

  9. dos for循环

    in (phone.txt) do ( 127.0.0.1 > tmp_file ) ) do ( 127.0.0.1 > tmp_file )

  10. B - Reverse and Compare 小小思维题

    http://agc019.contest.atcoder.jp/tasks/agc019_b 一开始的做法是, 用总数减去回文子串数目,因为回文子串怎么翻转都不影响答案. 然后,如果翻转afucka ...