poj3007 Organize Your Train part II(字典树)

来源:互联网 发布:淘宝店关闭了还能开吗 编辑:程序博客网 时间:2024/04/29 14:22


Organize Your Train part II
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8325 Accepted: 2382

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

/*

给定一个字符串,从任意位置把它切为两半,得到两条子串

定义 子串1为s1,子串2为s2,子串1的反串为s3,子串2的反串为s4

现在从s1 s2 s3 s4中任意取出两个串组合,问有多少种不同的组合方法

*/


我已开始用的是纯字典树,但是老是爆内存,逼不得已只能使用数组模拟字典树了!

代码如下:

#include <cstring>#include <cstdio>#include <algorithm>#include <iostream>using namespace std;struct tree{    int next[26];}a[78000];char ch[80];int sum;int top;int create(){    memset( a[top].next, -1, sizeof(a[top].next) );    return top++;}void Create( char *s ){    int len = strlen(s);    int i, r = 0;    int ok = 0;    for ( i = 0;i < len; i++ )    {        if ( a[r].next[s[i]-'a'] == -1 )        {            a[r].next[s[i]-'a'] = create();            ok = 1;        }        r = a[r].next[s[i]-'a'];    }    if ( ok == 1 )        sum++;}int main(){    int n;    int i, j, k;    scanf ( "%d", &n );    while ( n-- )    {        sum = 0;        memset( a[0].next, -1, sizeof(a[0].next) );        top = 1;        scanf ( "%s", ch );        int len = strlen(ch);        for ( i = 0;i < len; i++ )        {            char a1[80], a2[80], a3[80], a4[80];            for ( j = 0;j < i; j++ )                a1[j] = ch[j];            a1[j] = '\0';            for ( j = i; j < len; j++ )                a2[j-i] = ch[j];            a2[j-i] = '\0';            int len1 = strlen(a1);            for ( j = 0;j < len1; j++ )                a3[j] = a1[len1-j-1];            a3[j] = '\0';            int len2 = strlen(a2);            for ( j = 0;j < len2; j++ )                a4[j] = a2[len2-j-1];            a4[j] = '\0';            char b[10][80];            j = 0, k = 0;            while ( k < len1 )                b[0][j++] = a1[k++];            k = 0;            while ( k < len2 )                b[0][j++] = a2[k++];            b[0][j] = '\0';            j = 0, k = 0;            while ( k < len1 )                b[1][j++] = a1[k++];            k = 0;            while ( k < len2 )                b[1][j++] = a4[k++];            b[1][j] = '\0';            j = 0, k = 0;            while ( k < len1 )                b[2][j++] = a3[k++];            k = 0;            while ( k < len2 )                b[2][j++] = a2[k++];            b[2][j] = '\0';            j = 0, k = 0;            while ( k < len1 )                b[3][j++] = a3[k++];            k = 0;            while ( k < len2 )                b[3][j++] = a4[k++];            b[3][j] = '\0';            j = 0, k = 0;            while ( k < len2 )                b[4][j++] = a2[k++];            k = 0;            while ( k < len1 )                b[4][j++] = a1[k++];            b[4][j] = '\0';            j = 0, k = 0;            while ( k < len2 )                b[5][j++] = a2[k++];            k = 0;            while ( k < len1 )                b[5][j++] = a3[k++];            b[5][j] = '\0';            j = 0, k = 0;            while ( k < len2 )                b[6][j++] = a4[k++];            k = 0;            while ( k < len1 )                b[6][j++] = a3[k++];            b[6][j] = '\0';            j = 0, k = 0;            while ( k < len2 )                b[7][j++] = a4[k++];            k = 0;            while ( k < len1 )                b[7][j++] = a1[k++];            b[7][j] = '\0';            for ( j = 0;j < 8; j++ )                Create( b[j] );        }        printf ( "%d\n", sum );    }}


在这里附上一个纯字典树(爆内存的字典树)

#include <cstring>#include <cstdio>#include <algorithm>#include <iostream>using namespace std;struct tree{    char data;    struct tree *next[26];};char ch[80];int sum;void Create( char *str, struct tree *T ){    int i;    int ok = 0;    int id;    while ( *str )    {        id = *str-'a';        if ( T->next[id] == NULL )        {            T->next[id] = new tree;            for ( i = 0;i < 26; i++ )                T->next[id]->next[i] = NULL;            T->next[id]->data = *str;            T = T->next[id];            ok = 1;        }        else            T = T->next[id];        str++;    }    if ( ok == 1 )        sum++;}int main(){    int n;    int i, j, k;    scanf ( "%d", &n );    while ( n-- )    {        sum = 0;        struct tree *T = new tree;        for ( i = 0;i < 26; i++ )            T->next[i] = NULL;        scanf ( "%s", ch );        int len = strlen(ch);        for ( i = 0;i < len; i++ )        {            char a1[80], a2[80], a3[80], a4[80];            for ( j = 0;j < i; j++ )                a1[j] = ch[j];            a1[j] = '\0';            for ( j = i; j < len; j++ )                a2[j-i] = ch[j];            a2[j-i] = '\0';            int len1 = strlen(a1);            for ( j = 0;j < len1; j++ )                a3[j] = a1[len1-j-1];            a3[j] = '\0';            int len2 = strlen(a2);            for ( j = 0;j < len2; j++ )                a4[j] = a2[len2-j-1];            a4[j] = '\0';            char b[10][80];            j = 0, k = 0;            while ( k < len1 )                b[0][j++] = a1[k++];            k = 0;            while ( k < len2 )                b[0][j++] = a2[k++];            b[0][j] = '\0';            j = 0, k = 0;            while ( k < len1 )                b[1][j++] = a1[k++];            k = 0;            while ( k < len2 )                b[1][j++] = a4[k++];            b[1][j] = '\0';            j = 0, k = 0;            while ( k < len1 )                b[2][j++] = a3[k++];            k = 0;            while ( k < len2 )                b[2][j++] = a2[k++];            b[2][j] = '\0';            j = 0, k = 0;            while ( k < len1 )                b[3][j++] = a3[k++];            k = 0;            while ( k < len2 )                b[3][j++] = a4[k++];            b[3][j] = '\0';            j = 0, k = 0;            while ( k < len2 )                b[4][j++] = a2[k++];            k = 0;            while ( k < len1 )                b[4][j++] = a1[k++];            b[4][j] = '\0';            j = 0, k = 0;            while ( k < len2 )                b[5][j++] = a2[k++];            k = 0;            while ( k < len1 )                b[5][j++] = a3[k++];            b[5][j] = '\0';            j = 0, k = 0;            while ( k < len2 )                b[6][j++] = a4[k++];            k = 0;            while ( k < len1 )                b[6][j++] = a3[k++];            b[6][j] = '\0';            j = 0, k = 0;            while ( k < len2 )                b[7][j++] = a4[k++];            k = 0;            while ( k < len1 )                b[7][j++] = a1[k++];            b[7][j] = '\0';            for ( j = 0;j < 8; j++ )                Create( b[j], T );        }        printf ( "%d\n", sum );    }    return 0;}



代码菜鸟,如有错误,请多包涵!!!

如有帮助记得支持我一下,谢谢!!!

0 0
原创粉丝点击