zoj1109-Language of FatMouse 字典树
来源:互联网 发布:淘宝菲戈体育 编辑:程序博客网 时间:2024/05/16 08:16
Language of FatMouse
We all know that FatMouse doesn't speak English. But now he has to be prepared since our nation will join WTO soon. Thanks to Turing we have computers to help him.
Input Specification
Input consists of up to 100,005 dictionary entries, followed by a blank line, followed by a message of up to 100,005 words. Each dictionary entry is a line containing an English word, followed by a space and a FatMouse word. No FatMouse word appears more than once in the dictionary. The message is a sequence of words in the language of FatMouse, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.
Output Specification
Output is the message translated to English, one word per line. FatMouse words not in the dictionary should be translated as "eh".
Sample Input
dog ogdaycat atcaypig igpayfroot ootfrayloops oopslayatcayittenkayoopslay
Output for Sample Input
catehloops
题目大意:给你两个字符串a,b,询问字符串是不是和b相等,如果相等就输出字符串a,否则输出eh.
解题思路:
1、使用字典树,保存两个字符串,利用字典树判断是否存在第二个字符串,存在即输出第一个字符串。
2、使用map容器来存储字符串,如果存在该字符串即输出。
字典树专题:点击打开链接http://blog.csdn.net/wang_heng199/article/details/76448804
字典树ac代码:
#include <stdio.h> #include <string.h> #include <malloc.h>#include <iostream> using namespace std;struct Tree{ int num; char s[15]; struct Tree *next[27]; }; struct Tree *root; void Initialize() //初始化全部为空 { root = (struct Tree*)malloc(sizeof(struct Tree)); root->num = 0; for(int i = 0 ; i < 26 ; ++i) root->next[i] = NULL; } void Insert(char *s1,char *s2)//以s1为基础建立字典树 { int len = strlen(s1); struct Tree *node,*p = root; for(int i = 0 ; i < len ; ++i){ int pos = s1[i] - 'a'; if(p->next[pos] == NULL){ node = (struct Tree*)malloc(sizeof(struct Tree)); for(int j = 0 ; j < 26 ; ++j){ node->next[i] = NULL; node->num = 0; } p->next[pos] = node; } p = p->next[pos]; } p->num = 1; //标记为1 strcpy(p->s,s2); //存储字符串待输出 } void Find(char *s) { int len = strlen(s); int i; struct Tree *p = root; for(i = 0 ; i < len ; ++i){ int pos = s[i] - 'a'; if(p->next[pos] != NULL) p = p->next[pos]; else break; } if(p->num == 1) //为1即表示如果存在 printf("%s\n",p->s); else printf("eh\n"); } int main() { char str[30]; Initialize(); while(gets(str)){ if(strlen(str) == 0) break; char str1[12],str2[12]; memset(str1,0,sizeof(str1)); memset(str2,0,sizeof(str2)); for(int i = 0 ; i < strlen(str) ; ++i){ if(str[i] == ' '){ strncpy(str1,str,i); str1[i] = '\0'; strncpy(str2,str+i+1,strlen(str)-i-1); str2[strlen(str) - i - 1] = '\0'; } } Insert(str2,str1); } while(scanf("%s",str) != EOF) Find(str); return 0; }
STLac代码:
#include <cstdio>#include <cstring>#include <map>#include <algorithm>#include <iostream>using namespace std;char s1[20], s2[20], s3[20];char s[100007][15];int main(){ map<string, int>ma; int e=1; while(scanf("%c", &s1[0]) && s1[0]!='\n' ) { scanf("%s %s%*c", s1+1, s2 ); ma[s2]=e++ ; strcpy(s[e-1], s1); } int flag; while(scanf("%s", s3)!=EOF) { if(ma[s3]>=1) { printf("%s\n", s[ma[s3]] ); } if(ma[s3]==0) { printf("eh\n"); } } return 0;}题目链接:点击打开链接http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1109
- zoj1109-Language of FatMouse 字典树
- ZOJ1109 Language of FatMouse
- ZOJ1109 Language of FatMouse
- ZOJ1109 Language of FatMouse,BST
- zoj1109 水题(大神绕道) Language of FatMouse
- zoj 1109 Language of FatMouse 【字典树】
- zoj 1109 Language of FatMouse(字典树)
- zoj 1109 Language of FatMouse(字典树Trie)
- zoj 1109 Language of FatMouse (字典树Trie)
- Language of FatMouse(trie树)
- Language of FatMouse
- C - Language of FatMouse
- zoj_1109 Language of FatMouse
- 字典树例题介绍:ZOJ 1109 Language of FatMouse,HDOJ 1075hdoj What Are You Talking About
- 字典树 ZOJ1109 HDU1251 PKU1204 HDU1075
- ZOJ 1109 Language of FatMouse
- zoj 1109 Language of FatMouse
- ZOJ 1109Language of FatMouse
- html属性大全。
- codeforce-243C:Colorado Potato Beetle(BFS+离散化+暴力)
- RTMP协议在蓝牙连接中传输数据的运用
- TF-IDF模型的概率解释
- html空格
- zoj1109-Language of FatMouse 字典树
- 以太坊中的叔块(uncle block)
- Several ports (8005, 8080, 8009) required by Tomcat v5.5 Server at localhost are already in use.
- vc在线程序更新和如何更新更新程序update.exe
- POJ 2411 状压DP||组合数学
- DataGridView美化
- 奥威Power-BI数据可视化分析--客流量分析
- Fiddler plus 【高效调试分析利器】
- XYNU OJ 1095: 习题6-9 折半查找