uva10391Compound Words
来源:互联网 发布:156个援建项目 知乎 编辑:程序博客网 时间:2024/06/07 01:24
简单的哈希表运用。
把==写成了=wa了几次。。。唉,渣水平目不忍视啊,以后还是把常量写在==前面吧。。
补充一种字符串哈希函数
int Hash(char *str) // BKDR字符串哈希函数 { int seed = 131, sum = 0; while(*str) sum = sum * seed + (*str++); return (sum & 0x7FFFFFFF) % MAXSIZE; }
#include<cstdio>#include<cstring>#include<algorithm>#define HASHSIZE 1000000#define LENGTH 100#define MAX 121000using namespace std;char s[MAX][LENGTH];int next[MAX],head[HASHSIZE];int n;int hash(char s[LENGTH]){ int value=0,i,j; j=strlen(s); for(i=0;i<j;i++) { value=(value*10+s[i]-'a')%HASHSIZE; } //printf("%d ",value); return value;}void insert(int cur){ int hashvalue=hash(s[cur]); if(head[hashvalue]==-1)//错写成了= { head[hashvalue]=cur; next[cur]=-1; } else { next[cur]=head[hashvalue]; head[hashvalue]=cur; } return ;}int is(char a[LENGTH],char b[LENGTH]){ int hasha=hash(a),hashb=hash(b),ok=0,u; if(head[hasha]==-1||head[hashb]==-1) return 0; u=head[hasha]; while(u!=-1) { if(strcmp(s[u],a)==0) { ok=1; break; } u=next[u]; } if(ok==0) return 0; u=head[hashb]; while(u!=-1) { if(strcmp(s[u],b)==0) { return 1; } u=next[u]; } return 0;}void solve(){ int i,j,t,leng,la,lb; char a[LENGTH],b[LENGTH]; for(i=0;i<n;i++) { leng=strlen(s[i]); if(leng==1) continue; //printf("%d\n",leng); for(j=0;j<leng-1;j++) { la=lb=0; for(t=0;t<=j;t++) a[la++]=s[i][t]; a[la]=0; for(;t<leng;t++) b[lb++]=s[i][t]; b[lb]=0; //printf("1");puts(a);printf("2");puts(b); if(is(a,b)) { puts(s[i]); break; } } //printf("test"); }}int main(){ int i,j; n=0; memset(head,-1,HASHSIZE*4); memset(next,-1,MAX*4); while(scanf("%s",s[n])!=EOF) { //if(n!=0&&strcmp(s[n-1],s[n])==0)//去重 //{ // continue; //} //else //{ insert(n); n++; //} } //for(i=0;i<n;i++) // puts(s[i]);//printf("test"); solve(); return 0;}
0 0
- uva10391Compound Words
- words
- words
- Words
- words
- Words
- words
- words
- words
- Words
- words
- New words
- SOME WORDS
- cat words
- SCM Words
- no words~
- e words
- New Words
- Android 之开发环境的搭建
- hdu-2516 斐波那契数sg值
- 德国的嘎嘎是嘎啊是我情人无情无日期为任务
- 深入理解python之self
- Linux下安装*.bin/*.sh格式文件
- uva10391Compound Words
- 手贱 + 做事不认真想想后果 == 悲剧!
- Activity生命周期
- 常用的脚本
- sitemesh struts2 整合
- POJ 1905 Expanding Rods(几何+二分)
- MySQL索引背后的数据结构及算法原理
- Stack Overflow requires external JavaScript from another domain, which is blocked or failed to load.
- 前滚&后滚