3007

/*
利用字符串hash函数,一般常用的hash函数是BKDRhash.
*/

// include file
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <ctime>

#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <strstream>

#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <functional>

using namespace std;

// typedef
typedef long long LL;
typedef unsigned long long ULL;

// 
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define FORi(a,b,c) for(int i=(a);i<(b);i+=c)
#define FORj(a,b,c) for(int j=(a);j<(b);j+=c)
#define FORk(a,b,c) for(int k=(a);k<(b);k+=c)
#define FORp(a,b,c) for(int p=(a);p<(b);p+=c)

#define FF(i,a)    for(int i=0;i<(a);i+++)
#define FFD(i,a)   for(int i=(a)-1;i>=0;i--)
#define Z(a) (a<<1)
#define Y(a) (a>>1)

const double eps = 1e-6;
const double INFf = 1e10;
const int INFi = 1000000000;
const double Pi = acos(-1.0);

template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T TMAX(T x,T y)
{
	if(x>y) return x;
	return y;
}
template<class T> inline T TMIN(T x,T y)
{
	if(x<y) return x;
	return y;
}
template<class T> inline void SWAP(T &x,T &y)
{
	T t = x;
	x = y;
	y = t;
}
template<class T> inline T MMAX(T x,T y,T z)
{
	return TMAX(TMAX(x,y),z);
}


// code begin

int BKDRHash(char *tar)
{
	int seed = 131;
	int hash = 0;
	while(*tar)
	{
		hash = hash*seed + (*tar++);
	}
	return (hash&0x7FFFFFFF);
}
#define pri 99997
struct node
{
	char da[75];
	int next;
};
node mem[1000];
int L[pri];

int T;
char in[74],leftz[74],leftf[74],rightz[74],rightf[74],sumt[74];
int n;
int dx;

bool find(char* tar,int hash)
{
	int mdx = L[hash];
	while(mdx!=-1)
	{
		if(strcmp(mem[mdx].da,tar)==0)
			return true;
		mdx = mem[mdx].next;
	}
	return false;
}

void joint(char *st1,char *st2)
{
	int st1s = strlen(st1);
	int st2s = strlen(st2);
	for(int i=0;i<st1s;i++)
		sumt[i] = st1[i];
	for(int i=0;i<st2s;i++)
		sumt[i+st1s] = st2[i];

	sumt[st1s+st2s] = 0;

	int hash = BKDRHash(sumt)%pri;

	if(!find(sumt,hash))
	{
		strcpy(mem[dx].da,sumt);
		mem[dx].next = L[hash];
		L[hash] = dx++;
	}
}

int main()
{
	read;
	write;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%s",in);
		memset(L,-1,sizeof(L));
		dx = 0;	
		n = strlen(in);
		for(int i=0;i<n-1;i++)
		{
			//printf("%d\n",i);
			int j = 0;
			while(j<=i)
			{
				leftz[j] = in[j];
				leftf[i-j] = in[j];
				j++;
			}
			leftz[j] = 0;
			leftf[j] = 0;

			//printf("%s %s\n",leftz,leftf);
			j = i+1;
			while(j<n)
			{
				rightz[j-i-1] = in[j];
				rightf[n-1-j] = in[j];
				j++;
			}
			rightz[n-i-1] = 0;
			rightf[n-i-1] = 0;

			//printf("%s %s\n",rightz,rightf);
			
			joint(leftz,rightz);
			joint(leftz,rightf);
			joint(leftf,rightz);
			joint(leftf,rightf);
			joint(rightz,leftz);
			joint(rightz,leftf);
			joint(rightf,leftz);
			joint(rightf,leftf);
			
		}
		printf("%d\n",dx);
	}
	return 0;
}
posted @ 2011-03-22 19:22  AC2012  阅读(210)  评论(0编辑  收藏  举报