poj-2503-Babelfish-字典树orMAP or 字符串哈希
来源:互联网 发布:手机邮箱软件 编辑:程序博客网 时间:2024/06/13 07:46
题意:
给你一组字符串, 每一行有两个串,a,b;
输入空行结束
给你一个串c,问你在前面的第二列的数组中出没出现过,有的话,输出前面的那个,没有的话输出eh
sscanf函数应用是,把一个串,分割成不同类型的
1. map 2625ms
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<map>#include<queue>#include<climits>#include<list>#define MULT 20000using namespace std;int main(){ char ls[30], lk[30],lp[30]; map<string,string>mapS; while(gets(ls)) { if(strlen(ls)==0) break; sscanf(ls,"%s %s",lk,lp); mapS[lp] = lk; } map<string,string>::iterator iter; while(gets(ls)) { iter = mapS.find(ls); if(iter!=mapS.end()) { cout<<iter->second<<endl; } else printf("eh\n"); } return 0;}2 哈希 813ms
#include<stdio.h>#include<string.h>#include<stdlib.h>#define mod 2000int tmp ;using namespace std;struct node{ char s[30]; char a[30]; int next;}ls[100500];int head[2500];int hash_index(char s[]){ int hash=1; int len=strlen(s); for(int i=0; i<len; i++) hash=(hash*29+s[i]-'a')%mod; return hash;}int main(){ tmp = 1; char tr[30], tl[30], to[30]; memset(head,-1,sizeof(head)); while(gets(to)) { if(strlen(to)==0) break; sscanf(to,"%s %s",tl,tr); int key = hash_index(tr); if(head[key]==-1) { strcpy(ls[tmp].s,tl); strcpy(ls[tmp].a,tr); ls[tmp].next = -1; head[key] = tmp++; } else { int c = head[key]; while(1) { if(ls[c].next!=-1) c = ls[c].next; else break; } strcpy(ls[tmp].s,tl); strcpy(ls[tmp].a,tr); ls[tmp].next = -1; ls[c].next = tmp++; } } while(gets(tr)) { int key = hash_index(tr); int c = head[key]; if(head[key]==-1) { printf("eh\n"); continue; } while(c!=-1) { if(strcmp(ls[c].a,tr)==0) { printf("%s\n",ls[c].s); break; } c = ls[c].next; } if(c == -1) printf("eh\n"); } return 0;}
3. 字典树 532ms
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<algorithm>#include<iostream>using namespace std;int top;struct node{ int next[30]; int n; char s[30];} pos[150500];char ls[35];void add(char st[],int flag){ int j = 0, len = strlen(st); int i; for( i = 0; i < len; i++) { int x = st[i]-'a'+1; if(pos[j].next[x]==0) pos[j].next[x]=++top; j = pos[j].next[x]; } if(flag == 1) { if(pos[j].n == 0) { pos[j].n = 1; strcpy(pos[j].s,ls); } } else { if(pos[j].n== 0) printf("eh\n"); else printf("%s\n",pos[j].s); }}int main(){ top = 0; memset(pos,0,sizeof(pos)); char str[35], lk[35]; while(gets(str)) { if(strlen(str)==0) break; sscanf(str,"%s %s",ls,lk); add(lk,1); } while(gets(str)) { add(str,0); } return 0;}
0 0
- poj-2503-Babelfish-字典树orMAP or 字符串哈希
- poj 2503 Babelfish(hash or 字典树)
- POJ 2503 Babelfish qsort+bserach OR 字典树
- poj 2503 Babelfish(字典树)
- poj 2503 Babelfish 字典树
- poj 2503 Babelfish(字典树)
- POJ 2503 Babelfish(字典树)
- poj-2503-Babelfish-字典树
- poj 2503 Babelfish(字典树)
- POJ 2503 - Babelfish(字典树)
- poj 2503 Babelfish 字典树
- POJ 2503-Babelfish【字典树】
- Babelfish+poj+字典树
- Babelfish(字典树 or map)
- POJ 2503 Babelfish 字典树应用
- POJ 2503 Babelfish(字典树Trie)
- POJ - 2503 Babelfish 字典树做法
- POJ 2503 Babelfish(字典树)
- 算法竞赛入门经典:第七章 暴力求解法 7.5枚举排列
- UVA10288 Coupons(卡输出神题)
- MySQL 调优/优化的 100 个建议
- android 多线程下载断点续传
- codeforces 508E E. Arthur and Brackets(栈模拟+贪心+构造)
- poj-2503-Babelfish-字典树orMAP or 字符串哈希
- [搜索or枚举]AOJ 0525 Osenbei
- Python的函数参数
- 动态规划-0-1背包问题
- io流(一)
- 前端性能优化的14个规则,学会就偷着乐
- MySQL 高可用架构在业务层面的分析研究
- 光丝字,设计七夕主题光丝文字
- HDOJ 1874 畅通工程续(最短路径,dijkstra算法,spfa邻接表,floyd算法)