POJ 3007 - Organize Your Train part II

来源:互联网 发布:基于比较排序算法 编辑:程序博客网 时间:2024/05/17 08:31
Language:
Organize Your Train part II
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8086 Accepted: 2339

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

4aaabbaabcdabcde

Sample Output

161218

Source

Japan 2006 Domestic
Analysis
题目大意:给一个只包含小写字母的字符串数组,从任意位置分割,互相反转组合,问除去重复后的最多组合数。因为字符串的长度最大是72,所以直接暴力分割计数即可,用静态字典树记录出现过的状态。
不得不说,这题还真是坑,开始做的时候,看了半天不会做,于是去找题解,看到网上大神们有的用哈希,有的用平衡二叉树记录状态,于是想到用字典树做,代码量和思维都要简单不少,但是。。。

第一次MLE是因为我用的动态字典树,要申请大量的空间,于是我在每组结束时回收内存,于是就来了第二次的TLE,第三次和第四次我不回收内存了,直接初始化第一次申请的空间,第三次初始化的地方写错了,还是MLE,第四次写是写对了,又TLE了,可想而知我的内心是崩溃的,到这时候终于知道是用静态字典树做了,开了个100w的结构体数组,不知为啥还是MLE啊啊啊啊啊,我又删掉了一个0变成10w才过的,但是我看到网上别人做就是开的100w啊?????Why!!!!!
Source Code
#include <cstdio>#include <cstring>using namespace std;struct node{    int flag,next[26];} p[100086];int ans,top;void create(int x){    p[x].flag=0;    for (int i=0; i<26; i++)        p[x].next[i]=-1;}void add(int x,char *s,int l){    for (int i=0; i<l; i++)    {        if (p[x].next[s[i]-'a']<0)        {            p[x].next[s[i]-'a']=top;            create(top++);        }        x=p[x].next[s[i]-'a'];    }    if (!p[x].flag)    {        p[x].flag=1;        ans++;    }}int main(){    int t,l,i,j,k,q;    char s[128],s1[128],s2[128];    scanf("%d",&t);    while(t--)    {        top=ans=0;        create(top++);        scanf("%s",s);        l=(int)strlen(s);        for (i=0; i<l; i++)        {            k=q=0;            for (j=0; j<i; j++)                s1[k++]=s[j];            for (j=i; j<l; j++)                s2[q++]=s[j];            s1[k++]=s2[q++]=0;            strcat(s2,s1);            add(0,s2,(int)strlen(s2));            k=q=0;            for (j=i-1; j>=0; j--)                s1[k++]=s[j];            for (j=i; j<l; j++)                s2[q++]=s[j];            s1[k++]=s2[q++]=0;            strcat(s1,s2);            add(0,s1,(int)strlen(s1));            k=q=0;            for (j=i-1; j>=0; j--)                s1[k++]=s[j];            for (j=i; j<l; j++)                s2[q++]=s[j];            s1[k++]=s2[q++]=0;            strcat(s2,s1);            add(0,s2,(int)strlen(s2));            k=q=0;            for (j=0; j<i; j++)                s1[k++]=s[j];            for (j=l-1; j>=i; j--)                s2[q++]=s[j];            s1[k++]=s2[q++]=0;            strcat(s1,s2);            add(0,s1,(int)strlen(s1));            k=q=0;            for (j=0; j<i; j++)                s1[k++]=s[j];            for (j=l-1; j>=i; j--)                s2[q++]=s[j];            s1[k++]=s2[q++]=0;            strcat(s2,s1);            add(0,s2,(int)strlen(s2));            k=q=0;            for (j=i-1; j>=0; j--)                s1[k++]=s[j];            for (j=l-1; j>=i; j--)                s2[q++]=s[j];            s1[k++]=s2[q++]=0;            strcat(s1,s2);            add(0,s1,(int)strlen(s1));        }        printf("%d\n",ans);    }    return 0;}


1 0
原创粉丝点击