Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8523 | Accepted: 2435 |
Description
RJ Freight, a Japanese railroad company for freight operations has recently constructed exchange lines at Hazawa, Yokohama. The layout of the lines is shown in Figure 1.
Figure 1: Layout of the exchange lines
A freight train consists of 2 to 72 freight cars. There are 26 types of freight cars, which are denoted by 26 lowercase letters from "a" to "z". The cars of the same type are indistinguishable from each other, and each car's direction doesn't matter either. Thus, a string of lowercase letters of length 2 to 72 is sufficient to completely express the configuration of a train.
Upon arrival at the exchange lines, a train is divided into two sub-trains at an arbitrary position (prior to entering the storage lines). Each of the sub-trains may have its direction reversed (using the reversal line). Finally, the two sub-trains are connected in either order to form the final configuration. Note that the reversal operation is optional for each of the sub-trains.
For example, if the arrival configuration is "abcd", the train is split into two sub-trains of either 3:1, 2:2 or 1:3 cars. For each of the splitting, possible final configurations are as follows ("+" indicates final concatenation position):
[3:1] abc+d cba+d d+abc d+cba [2:2] ab+cd ab+dc ba+cd ba+dc cd+ab cd+ba dc+ab dc+ba [1:3] a+bcd a+dcb bcd+a dcb+a
Excluding duplicates, 12 distinct configurations are possible.
Given an arrival configuration, answer the number of distinct configurations which can be constructed using the exchange lines described above.
Input
The entire input looks like the following.
the number of datasets = m
1st dataset
2nd dataset
...
m-th dataset
Each dataset represents an arriving train, and is a string of 2 to 72 lowercase letters in an input line.
Output
For each dataset, output the number of possible train configurations in a line. No other characters should appear in the output.
Sample Input
4 aa abba abcd abcde
Sample Output
1 6 12 18
Source
题目意思
解题思路
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define INF 0xfffffff
const int MAXN=99991;
const int MAXM=100;
char c[MAXM];//原始串
char s1[MAXM],s1[MAXM];//分割的两个串
char t1[MAXM],t2[MAXM];//保留原始分割的两个串
char s[MAXM];//最终连结的串
//map<std::string,bool> ma;
void reserve(char x[])//反转字符串
{
char temp[MAXN];
int n=strlen(x),cnt=0;
for(int i=n-1; i>=0; --i)
temp[cnt++]=x[i];
temp[cnt]='\0';
strcpy(x,temp);
}
typedef struct node
{
char str[MAXM];
node *next;
node()
{
next=0;
}
} Point;
Point *Head[MAXN];
bool Hash(char *s) //哈希处理冲突
{
int len=strlen(s);
int num=0;
for(int i=0; i<len; i++)
{
num=num+s[i]*(i+1);
num%=MAXN;
}
if(Head[num]==NULL)
{
Point*p=new Point;
strcpy(p->str,s);
Head[num]=p;
return true;
}
else
{
Point *p = Head[num];
while(p)
{
if(strcmp(p->str,s)==0) return false;
p=p->next;
}
p=new Point;
strcpy(p->str,s);
p->next = Head[num];
Head[num] = p;
}
return true;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("G:/cbx/read.txt","r",stdin);
//freopen("G:/cbx/out.txt","w",stdout);
#endif
//ios::sync_with_stdio(false);
//cin.tie(0);
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",&c);
int len=strlen(c);
int ans=0;
for(int k=1; k<len; ++k)//分割点
{
memset(s1,'\0',sizeof(s1));
int cnt1=0;//第一串长度
for(int i=0; i<k; ++i)//第一串
s1[cnt1++]=c[i];
cnt1=strlen(s1);
memset(s2,'\0',sizeof(s2));
int cnt2=0;//第二串长度
for(int i=k; i<len; ++i)//第二串
s2[cnt2++]=c[i];
memcpy(t1,s1,cnt1);
memcpy(t2,s2,cnt2);
//上上
reserve(s1);
reserve(s2);
int cnt=0;
for(int i=0; i<cnt1; ++i)
s[cnt++]=s1[i];
for(int i=0; i<cnt2; ++i)
s[cnt++]=s2[i];
//cout<<s<<" "<<s1<<" "<<s2<<endl;
if(Hash(s)) ++ans;//记录
cnt=0;
for(int i=0; i<cnt2; ++i)
s[cnt++]=s2[i];
for(int i=0; i<cnt1; ++i)
s[cnt++]=s1[i];
//cout<<s<<" "<<s1<<" "<<s2<<endl;
if(Hash(s)) ++ans;//记录
//下下
memcpy(s1,t1,cnt1);
memcpy(s2,t2,cnt2);
cnt=0;
for(int i=0; i<cnt1; ++i)
s[cnt++]=s1[i];
for(int i=0; i<cnt2; ++i)
s[cnt++]=s2[i];
//cout<<s<<" "<<s1<<" "<<s2<<endl;
if(Hash(s)) ++ans;//记录
cnt=0;
for(int i=0; i<cnt2; ++i)
s[cnt++]=s2[i];
for(int i=0; i<cnt1; ++i)
s[cnt++]=s1[i];
//cout<<s<<" "<<s1<<" "<<s2<<endl;
if(Hash(s)) ++ans;//记录
//上下
memcpy(s1,t1,cnt1);
memcpy(s2,t2,cnt2);
reserve(s1);
cnt=0;
for(int i=0; i<cnt2; ++i)
s[cnt++]=s2[i];
for(int i=0; i<cnt1; ++i)
s[cnt++]=s1[i];
//cout<<s<<" "<<s1<<" "<<s2<<endl;
if(Hash(s)) ++ans;//记录
cnt=0;
for(int i=0; i<cnt1; ++i)
s[cnt++]=s1[i];
for(int i=0; i<cnt2; ++i)
s[cnt++]=s2[i];
//cout<<s<<" "<<s1<<" "<<s2<<endl;
if(Hash(s)) ++ans;//记录
//下上
memcpy(s1,t1,cnt1);
memcpy(s2,t2,cnt2);
reserve(s2);
cnt=0;
for(int i=0; i<cnt1; ++i)
s[cnt++]=s1[i];
for(int i=0; i<cnt2; ++i)
s[cnt++]=s2[i];
//cout<<s<<" "<<s1<<" "<<s2<<endl;
if(Hash(s)) ++ans;//记录
cnt=0;
for(int i=0; i<cnt2; ++i)
s[cnt++]=s2[i];
for(int i=0; i<cnt1; ++i)
s[cnt++]=s1[i];
//cout<<s<<" "<<s1<<" "<<s2<<endl;
if(Hash(s)) ++ans;//记录
}
printf("%d\n",ans);
}
return 0;
}