Organize Your Train part II 字典树(此题专卡STL)

探讨一种特殊的火车重组问题,列车在特定线路中通过分割、反转和重新连接的方式形成新的配置,旨在找出所有可能的不重复配置数量。算法挑战在于高效地计算这些配置,使用字典树数据结构而非传统的字符串和集合操作。
Organize Your Train part II
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8787 Accepted: 2490

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

 

set 和 string 都不让用,只能手写strcat

#include <iostream>
#include <cstdio>
#include<set>
#include <vector>
#include <cstring>
#include <list>
#include <queue>
#include <algorithm>
#include<functional>
#include <stack>
#include<string>
const int MAXN = 1e5 + 9;
#define INF 0x3f3f3f3f
const unsigned long long P = 163;
typedef unsigned long long ULL;
using namespace std;
//set + string 瞎搞被卡了
//用字典树做
struct node
{
    bool been;
    int index;
    int Next[26];
};
node a[MAXN];
int tot = 0, ans = 0;
inline int new_node()
{
    for (int i = 0; i < 26; i++)
        a[tot].Next[i] = -1;
    a[tot].index = tot;
    a[tot].been = false;
    return tot++;
}
void insert(const char s[], int l, int p)
{
    int tmp = 0, cnt = 0;
    while (cnt < l)
    {
        int k = s[cnt] - 'a';
        if (a[p].Next[k] == -1)
            a[p].Next[k] = new_node();
        p = a[p].Next[k];
        cnt++;
    }
    if (!a[p].been)
        a[p].been = 1, ans++;
}
char tmp[13][100];
void cat(int i, int r, int L, char* a, char* b, char* c)
{
    int p = 0;
    for (int j = 0; j < i; j++)
        c[p++] = a[j];
    for (int j = 0; j < L - i; j++)
        c[p++] = b[j];
    insert(c, L, r);
}
int main()
{
    ios::sync_with_stdio(0);
    int T;
    cin >> T;
    while (T--)
    {
        ans = 0;
        tot = 0;
        int root = new_node();
        char str[MAXN];
        cin >> str;
        int L = strlen(str);
        if (L == 1)
        {
            cout << 1 << endl;
            continue;
        }
        for (size_t i = 1; i < L; i++)
        {
            for (int j = 0; j < i; j++)
                tmp[1][j] = str[j];
            for (int k = i; k < L; k++)
                tmp[2][k-i] = str[k];
            for (int j = 0; j < i; j++)//1-3
                tmp[3][j] = tmp[1][i - 1 - j];
            for (int k = 0; k < L - i; k++)//2-4
                tmp[4][k] = tmp[2][L - i - 1 - k];
            cat(i, root, L, tmp[1], tmp[2], tmp[5]);
            cat(i, root, L, tmp[1], tmp[4], tmp[5]);
            cat(L - i, root, L, tmp[2], tmp[1], tmp[5]);
            cat(L - i, root, L, tmp[2], tmp[3], tmp[5]);
            cat(i, root, L, tmp[3], tmp[2], tmp[5]);
            cat(i, root, L, tmp[3], tmp[4], tmp[5]);
            cat(L - i, root, L, tmp[4], tmp[1], tmp[5]);
            cat(L - i, root, L, tmp[4], tmp[3], tmp[5]);
            
        }
        cout << ans << endl;
    }
}

 

转载于:https://www.cnblogs.com/joeylee97/p/7552212.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值