poj 3087 模拟(ELFHash模版,字符串哈希)
2012-10-19 16:51
429 查看
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define MAXN 102 int N,C; char s1[MAXN],s2[MAXN],s12[MAXN<<1],ans[MAXN<<1]; //*****************ELFhash*********** #define HASH 9901 char table[9901][MAXN]; int len; int ELFHash(char a[MAXN]) { int h = 0; int x = 0; for(int i=0;i<len;++i) { h = (h << 4) + (a[i]); if ((x = h & 0xF0000000L) != 0) { h ^= (x >> 24); h &= ~x; } } return h % HASH; } bool is_in(char a[]) //ELFHash函数对字符串判重,没有的时候插入 { int i=ELFHash(a); while(strlen(table[i])>0&&strcmp(a,table[i])!=0) i=(i+1)%HASH; if(strlen(table[i])==0) { strcpy(table[i],a); return false; } return true; } //*********************************** int main() { scanf("%d",&N); for(int cas=1;cas<=N;cas++) { scanf("%d",&C);getchar(); len=C*2; memset(s1,0,sizeof(s1)); memset(s2,0,sizeof(s2)); memset(s12,0,sizeof(s12)); memset(ans,0,sizeof(ans)); memset(table,0,sizeof(table)); gets(s1); gets(s2); gets(ans); printf("%d ",cas); int step=0; while(1) { step++; for(int i=0;i<len;i+=2) s12[i]=s2[i/2]; for(int i=1;i<len;i+=2) s12[i]=s1[i/2]; s12[len]='\0'; //cout<<s1<<endl; //cout<<s2<<endl; if(strcmp(ans,s12)==0) { printf("%d\n",step); break; } else if(is_in(s12)) { printf("-1\n"); break; } for(int i=0;i<C;i++) s1[i]=s12[i]; for(int i=0;i<C;i++) s2[i]=s12[i+C]; } } return 0; }
相关文章推荐
- POJ-3087- Shuffle'm Up(字符串模拟)
- POJ 3087 模拟+hash
- POJ 3087 模拟+hash
- POJ 3087 Shuffle'm Up(字符串处理,模拟)
- 哈希(hash) 之 ELFHash 和 静态hash(模拟链接法)
- POJ 2406 Power Strings--字符串哈希(bkdhash)-KMP(next)
- 字符串哈希之ELFHash,poj2503
- POJ - 3087 两个字符串的组合能否到达另一个字符串
- poj 3602-字符串模拟
- 字符串Hash函数(ELFhash)
- poj 3087 直接模拟
- 字符串哈希之Rabin-Karp,poj1200
- 【POJ 3007】 Organize Your Train part II (字符串HASH)
- 【Unity】API : Animator.StringToHash 字符串到哈希
- ZOJ 3826 hash+字符串模拟
- POJ 3349 Snowflake Snow Snowflakes(哈希hash)
- poj 3087 直接模拟
- POJ 3573 I18n (字符串模拟)
- poj 3461 字符串单串匹配--KMP或者字符串HASH
- poj1200 Crazy Search——字符串哈希