标签:des style blog http color io os ar strong
Time Limit: 1000MS | Memory Limit: 65536K | |
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
1 #include<set> 2 #include<map> 3 #include<queue> 4 #include<cstdio> 5 #include<cstdlib> 6 #include<cstring> 7 #include<algorithm> 8 using namespace std; 9 const int N=30; 10 const int MAXN=80; 11 #define For(i,n) for(int i=1;i<=n;i++) 12 #define Rep(i,l,r) for(int i=l;i<=r;i++) 13 #define Down(i,r,l) for(int i=r;i>=l;i--) 14 15 struct trie{ 16 int sz,ch[N*MAXN*10][N],v[N*MAXN*10]; 17 void clr(){sz=1;memset(v,0,sizeof(v));memset(ch,0,sizeof(ch));} 18 int insert(char st[]){ 19 int now=v[1]=1,len=strlen(st); 20 Rep(i,0,len-1){ 21 int id=st[i]-‘a‘; 22 if(!ch[now][id]) ch[now][id]=++sz; 23 v[now=ch[now][id]]++; 24 } 25 if(v[now]>=2) return 0; 26 else return 1; 27 } 28 }trie; 29 30 char word[MAXN],prefix[MAXN],suffix[MAXN],New[MAXN]; 31 int n,T,ans; 32 33 void Reverse(char *s){ 34 int len=strlen(s); 35 Rep(i,0,(len-1)/2){ 36 char t=s[i];s[i]=s[len-i-1];s[len-i-1]=t; 37 } 38 } 39 40 void solve(){ 41 n=strlen(word); 42 Down(i,n-1,1){ 43 strncpy(prefix,word,i);prefix[i]=‘\0‘; 44 Rep(l,0,1){ 45 if(l==1&&(i==1)) continue; 46 if(l) Reverse(prefix); 47 Rep(k,i,n-1) suffix[k-i]=word[k];suffix[n-i]=‘\0‘; 48 Rep(j,0,1){ 49 if(j==1&&(i==n-1)) continue; 50 if(j) Reverse(suffix); 51 strcpy(New,prefix);strcat(New,suffix); 52 ans+=trie.insert(New); 53 strcpy(New,suffix);strcat(New,prefix); 54 ans+=trie.insert(New); 55 } 56 } 57 } 58 printf("%d\n",ans);ans=0; 59 } 60 61 int main(){ 62 scanf("%d",&T); 63 For(i,T){ 64 trie.clr(); 65 scanf("%s",&word); 66 solve(); 67 } 68 return 0; 69 }
POJ3007(Organize Your Train part II)
标签:des style blog http color io os ar strong
原文地址:http://www.cnblogs.com/kingnight/p/3969288.html