poj 3007 Organize Your Train part II(静态字典树哈希)

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6700 Accepted: 1922

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.

poj 3007 Organize Your Train part II(静态字典树哈希)
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
题意很简单,把一个字符串拆成两部分,每个字符串都可以逆置也可以两部分自由组合,这样就有8种组合方式,问不重复的字符串有几个;
原本很水的一个题,最后被我做的面目全非。一开始一直MLE,我就写了个DELETE函数将内存释放掉;接着就一直TLE到死,最后看了别人的解题报告说字典树是用空间换时间,是不会TLE的,原因在于malloc函数重复使用,所以就申请一个静态的结构体数组,这样才终于A了;
  1 #include<stdio.h>

  2 #include<string.h>

  3 #include<stdlib.h>

  4 #include<algorithm>

  5 using namespace std;

  6 

  7 struct Tree

  8 {

  9     bool flag;

 10     struct Tree *next[26];

 11 }mem[100000];

 12 int size;

 13 

 14 struct Tree *new_tree()

 15 {

 16     return mem+(size++);

 17 }

 18 

 19 void insert(struct Tree *tree,char s[])

 20 {

 21     int i,t;

 22     struct Tree *p = tree;

 23     for(i = 0; s[i]; i++)

 24     {

 25         t = s[i]-'a';

 26         if(p->next[t] == NULL)

 27         {

 28             Tree *q = new_tree();

 29             q->flag = 0;

 30             memset(q->next,0,sizeof(q->next));

 31             p->next[t] = q;

 32             p = q;

 33         }

 34         else

 35         p = p->next[t];

 36     }

 37     p->flag = 1;

 38 }

 39 

 40 int search(struct Tree *tree, char s[])

 41 {

 42     struct Tree *p = tree;

 43     int t,i;

 44     for(i = 0; s[i]; i++)

 45     {

 46         t = s[i]-'a';

 47         if(p->next[t] == NULL)

 48         {

 49             insert(tree,s);

 50             return 1;

 51         }

 52         p = p->next[t];

 53     }

 54     if(p->flag)

 55         return 0;

 56     insert(tree,s);

 57     return 1;

 58 }

 59 int main()

 60 {

 61     int item,i,j,len;

 62     char s[100];

 63     char str[100];

 64 

 65     scanf("%d",&item);

 66     while(item--)

 67     {

 68         size = 0;

 69         struct Tree *tree;

 70         int cnt = 1;

 71         scanf("%s",s);

 72         len = strlen(s);

 73 

 74         tree = new_tree();

 75         for(i = 0; i < 26; i++)

 76             tree->next[i] = NULL;

 77 

 78         insert(tree,s);

 79         for(i = 1; i < len; i++)

 80         {

 81             for(j = 0; j < len; j++)//+1-2

 82             {

 83                 if(j < i)  str[j] = s[j];

 84                 else str[j] = s[len-1-j+i];

 85             }

 86             str[len] = '\0';

 87             cnt += search(tree,str);

 88 

 89             for(j = 0; j < len; j++)//-1+2

 90             {

 91                 if(j < i)  str[j] = s[i-1-j];

 92                 else str[j] = s[j];

 93             }

 94             str[len] = '\0';

 95             cnt += search(tree,str);

 96 

 97             for(j = 0; j < len; j++)//-1-2

 98             {

 99                 if(j < i)  str[j] = s[i-1-j];

100                 else str[j] = s[len-1-j+i];

101             }

102             str[len] = '\0';

103             cnt += search(tree,str);

104 

105 

106             for(j = 0; j < len; j++)//+2+1

107             {

108                 if(j < len-i)  str[j] = s[i+j];

109                 else str[j] = s[j+i-len];

110             }

111             str[len] = '\0';

112             cnt += search(tree,str);

113 

114              for(j = 0; j < len; j++)//+2-1

115             {

116                 if(j < len-i)  str[j] = s[i+j];

117                 else str[j] = s[len-j-1];

118             }

119             str[len] = '\0';

120             cnt += search(tree,str);

121 

122 

123              for(j = 0; j < len; j++)//-2+1

124             {

125                 if(j < len-i)  str[j] = s[len-1-j];

126                 else str[j] = s[j+i-len];

127             }

128             str[len] = '\0';

129             cnt += search(tree,str);

130 

131             for(j = 0; j < len; j++)//-2-1

132             {

133                 if(j < len-i)  str[j] = s[len-1-j];

134                 else str[j] = s[len-1-j];

135             }

136             str[len] = '\0';

137             cnt += search(tree,str);

138         }

139         printf("%d\n",cnt);

140     }

141     return 0;

142 }
View Code

 

你可能感兴趣的:(part)