zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    LuoguP2763 试题库问题(最大流)
    LuoguP3254 圆桌问题(最大流)
    LuoguP2765 魔术球问题(最大流)
    LuoguP2764 最小路径覆盖问题(最大流)
    LuoguP4016 负载平衡问题(费用流)
    LuoguP2756 飞行员配对方案问题(最大流)
    BZOJ3675: [Apio2014]序列分割(斜率优化Dp)
    BZOJ1814: Ural 1519 Formula 1(插头Dp)
    BZOJ4652: [Noi2016]循环之美(莫比乌斯反演,杜教筛)
    BZOJ4916: 神犇和蒟蒻(杜教筛)
  • 原文地址:https://www.cnblogs.com/joeylee97/p/7552212.html
Copyright © 2011-2022 走看看