Organize Your Train part II
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.
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.
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 |
提示
题意:
给出一个字符串,不打乱顺序任意分成2份,生成子串1,子串2,且这2份接着又颠倒顺序生成反串1,反串2,之后任意取两个进行组合有几种情况。
组合的规则:
不能和自己的子串或反串组合,比如子串1不能和反串1结合。
但串i和串j,串j和串i是两者不同的方法,但不保证它们不相同。
思路:
个人觉得哈希,字典树(静态),二叉排序树都能做,但我只有二叉排序树过了,其他都TLE。
这是个悲哀的故事。
示例程序
Source Code
Problem: 3007 Code Length: 1823B
Memory: 3248K(472K) Time: 188MS(532MS) //括号里的数值是进行回收内存的情况
Language: GCC Result: Accepted
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct tree
{
char ch[145];
struct tree *l,*r;
};
int num;
struct tree *insert(char s[],struct tree *t)
{
int i;
if(t==NULL)
{
t=(struct tree *)malloc(sizeof(struct tree));
t->l=NULL;
t->r=NULL;
strcpy(t->ch,s);
num++;
}
else if(strcmp(t->ch,s)>0)
{
t->r=insert(s,t->r);
}
else if(strcmp(t->ch,s)<0)
{
t->l=insert(s,t->l);
}
return t;
}
int main()
{
int i,i1,i2,i3,len,t;
char ch[73],s1[5][73],s[145];
scanf("%d",&t);
for(i=1;t>=i;i++)
{
num=0;
struct tree *h=NULL;
scanf("%s",ch);
len=strlen(ch);
for(i1=1;len>i1;i1++)
{
for(i2=0;len>i2;i2++)
{
if(i1>i2)
{
s1[0][i2]=ch[i2];
}
else
{
s1[1][i2-i1]=ch[i2];
}
}
s1[0][i1]='\0'; //子串1
s1[1][len-i1]='\0'; //反串1
for(i2=len-1;i2>=0;i2--)
{
if(i2>=i1)
{
s1[3][len-1-i2]=ch[i2];
}
else
{
s1[2][i1-1-i2]=ch[i2];
}
}
s1[3][len-i1]='\0'; //反串2
s1[2][i1]='\0'; //子串2
for(i2=0;4>i2;i2++)
{
for(i3=0;4>i3;i3++)
{
if((i2+i3)%2!=0) //保证i2!=i3且不会和自己的子串或反串相结合
{
strcpy(s1[4],s1[i2]); //s1[4]用来过渡
strcpy(s,strcat(s1[4],s1[i3]));
h=insert(s,h);
}
}
}
}
printf("%d\n",num);
}
return 0;
}
练习赛时发现我竟然只用数组模拟就过了:
Source Code
Problem: 3007 Code Length: 1632B
Memory: 492K Time: 329MS
Language: GCC Result: Accepted
#include <stdio.h>
#include <string.h>
int main()
{
int i,i1,i2,i3,i4,t,pos,top;
char a[80],ch[4][80],temp[145],q[1000][145];
scanf("%d",&t);
for(i=1;t>=i;i++)
{
scanf("%s",a);
top=0;
for(i1=1;strlen(a)>i1;i1++)
{
for(i2=0;i1>i2;i2++)
{
ch[0][i2]=a[i2];
}
ch[0][i2]='\0';
pos=i2-1;
for(i2=0;pos>=0;i2++)
{
ch[1][i2]=a[pos];
pos--;
}
ch[1][i2]='\0';
for(i2=i1;strlen(a)>i2;i2++)
{
ch[2][i2-i1]=a[i2];
}
ch[2][i2-i1]='\0';
pos=i2-1;
for(i2=0;pos>=i1;i2++)
{
ch[3][i2]=a[pos];
pos--;
}
ch[3][i2]='\0';
for(i2=0;4>i2;i2++)
{
for(i3=0;4>i3;i3++)
{
if(i2!=i3&&i2+i3!=1&&i2+i3!=5)
{
strcat(strcpy(temp,ch[i2]),ch[i3]);
for(i4=0;top>i4;i4++)
{
if(strcmp(q[i4],temp)==0)
{
break;
}
}
if(i4==top)
{
strcpy(q[top],temp);
top++;
}
}
}
}
}
printf("%d\n",top);
}
return 0;
}