//poj 3007
//sep9
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN=1024;
const int HASHLEN=1000024;
int n,cnt;
char s[MAXN],s1[MAXN],s2[MAXN],s3[MAXN],s4[MAXN];
struct HashNode{
char str[MAXN];
HashNode *nxt;
};
HashNode* head[HASHLEN];
void insert(char *a,char *b)
{
char c[MAXN];
strcpy(c,a);
strcat(c,b);
int len=strlen(c);
int sum=0;
for(int i=0;i<len;++i)
sum+=c[i]*i;
for(HashNode* p=head[sum];p;p=p->nxt){
bool same=true;
for(int i=0;i<len;++i)
if((p->str)[i]!=c[i]){
same=false;
break;
}
if(same)
return ;
}
++cnt;
HashNode* now = new HashNode();
strcpy(now->str,c);
now->nxt=head[sum];
head[sum]=now;
}
int main()
{
scanf("%d",&n);
while(n--){
cnt=0;
scanf("%s",s);
memset(head,0,sizeof(head));
int len=strlen(s);
for(int i=1;i<len;++i){
int j=0,k=0;
for(;j<i;++j)
s1[j]=s[j];
s1[j]='\0';
for(j=i;j<len;++j)
s2[k++]=s[j];
s2[k]='\0';
strcpy(s3,s1);
strcpy(s4,s2);
reverse(s3,s3+i);
reverse(s4,s4+k);
insert(s1,s2);
insert(s2,s1);
insert(s3,s2);
insert(s2,s3);
insert(s1,s4);
insert(s4,s1);
insert(s3,s4);
insert(s4,s3);
}
printf("%d\n",cnt);
}
return 0;
}
poj 3007 Organize Your Train part II 哈希判重
最新推荐文章于 2017-04-10 16:36:06 发布