POJ 2503 Babelfish
来源:互联网 发布:mac草莓红和番茄红对比 编辑:程序博客网 时间:2024/06/07 01:20
Babelfish
题目链接:http://poj.org/problem?id=2503
~~~字符串哈希,处理空行!!!!!
~一开始不知道怎么处理空行跳出循环,这下涨姿势了~~
~字符串hash,用个hash函数就可以解决,我这里用的是BKDRHash(),其他的也应该可以,不过我没试过,目的就是求一个hash值出来,然后用next指针去处理冲突的情况,把hash值相同的都存在一条链上,因为本来冲突就少,所以不用担心查找的时候超时,vector不知道会不会超时,不过用链表的话还是更高效的,虽然没有vector方便,但也不复杂。
题目大意:翻译字符串,看下样例应该能懂,如果找不到这个字符串就输出“eh”;
AC代码:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define N 100010struct Word{ char str1[15]; char str2[15]; Word *next;}w[N];int BKDRHash(char *str){ int seed=31,key=0; while(*str){ key=seed*key+*str++; } return key&0x7fffffff;}void in(char *s1,char *s2){ int key=BKDRHash(s2)%N; Word *p=new Word(); strcpy(p->str1,s1); strcpy(p->str2,s2); p->next=w[key].next; w[key].next=p;}void Search(char *s2){ int key=BKDRHash(s2)%N; Word *p=w[key].next; while(p){ if(strcmp(p->str2,s2)==0){ printf("%s\n",p->str1); return; } p=p->next; } printf("eh\n");}int main(){ char s1[30],s2[30]; int i,j; memset(w,0,sizeof(w)); while(gets(s1)&&s1[0]){ for(i=0;s1[i];i++){ if(s1[i]==' '){ s1[i]='\0'; break; } } j=0; for(i=i+1;s1[i];i++,j++){ s2[j]=s1[i]; } s2[j]='\0'; in(s1,s2); } while(gets(s2)&&s2[0]){ Search(s2); } return 0;}
0 0
- poj 2503 Babelfish
- POJ 2503 Babelfish(HASH)
- poj 2503 Babelfish
- POJ 2503 Babelfish
- POJ 2503 Babelfish
- poj 2503 Babelfish
- POJ 2503 Babelfish
- poj 2503 Babelfish 【hash】
- poj 2503 Babelfish【trie】
- POJ 2503 Babelfish
- poj-2503 Babelfish
- poj-2503 Babelfish
- poj 2503 Babelfish
- poj 2503 Babelfish
- POJ 2503 Babelfish
- POJ 2503 Babelfish (Hash)
- POJ 2503 Babelfish
- poj 2503 Babelfish(二分)
- Tomcat类加载机制
- Java异常处理思路(try-catch使用时机)
- JVM监控工具
- intellij idea 使用:can't use subversion command line client : svn
- http://www.hexanon.net/
- POJ 2503 Babelfish
- redis源码分析----main函数
- [最短路径] HDU 1874 - 畅通工程续
- "error while loading shared libraries: xxx.so.x" 错误的原因和解决办法
- 通过Stack Overflow标签分析编程语言新趋势
- SIP learning
- androidpn源码地址
- A C# Winform Desktop App with Access to Remote Sql Server
- JAVA基础中的运算和表达式