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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: eslint-plugin-organize-imports是一个用于ESLint的插件,它可以帮助我们自动整理和优化我们的导入语句。 在编写JavaScript代码时,我们通常需要引入其他模块或库的功能。在大型项目中,可能会有许多导入语句,而这些语句的顺序和组织方式可能会导致代码的可读性和维护性下降。 eslint-plugin-organize-imports提供了一种自动化的方式来整理和优化导入语句。通过在代码中添加特定的注释指令,我们可以告诉该插件如何组织我们的导入语句。例如,我们可以使用注释指令`/* organize-imports */`来启用导入语句的整理功能。 一旦启用了eslint-plugin-organize-imports,它将扫描我们的代码并分析我们的导入语句。然后,它会根据一些配置规则和选项,对导入语句进行排序、分组和优化。 这个插件可以处理各种类型的导入语句,包括默认导入、命名导入和命名空间导入。它可以将相似的导入语句合并到同一个组中,删除不需要的导入语句,并根据规则对它们进行排序。这样,我们就可以更清楚地看到我们的模块依赖,并提高代码的可读性。 总结起来,eslint-plugin-organize-imports是一个帮助我们自动整理和优化导入语句的ESLint插件。它可以根据我们的配置规则对导入语句进行排序、分组和优化,从而提高代码的可读性和维护性。 ### 回答2: eslint-plugin-organize-imports是一个ESLint插件,用于自动整理和规范化import语句。它可以帮助开发者在代码中保持一致的import风格和结构,提高代码的可读性和维护性。 该插件提供了一系列规则,用于检测和修复import语句的错误和风格问题。它可以自动排序和格式化import语句,使其按照一定的顺序和规范排列。例如,可以按照字母顺序对import语句进行排序,将默认导出放在非默认导出之前,将外部模块的导入语句放在内部模块之前等等。通过使用该插件,可以确保在不同的文件和团队成员之间保持一致的import风格,减少代码冲突和维护难度。 除了排序和格式化import语句,eslint-plugin-organize-imports还提供了其他的规则,如删除未使用的导入、合并重复的导入、使用正确的导入别名等。这些规则可以帮助开发者减少无用的导入语句和重复的代码,提高代码质量和执行效率。 总之,eslint-plugin-organize-imports是一个功能强大的ESLint插件,可以帮助开发者自动整理和规范化import语句。它可以提高代码的可读性和维护性,减少冲突和错误的发生,是一个非常实用的工具。 ### 回答3: eslint-plugin-organize-imports 是一个 ESLint 插件,用于帮助开发者组织和管理 JavaScript 代码文件的 import 语句。 在 JavaScript 开发中,我们经常需要引入其他模块或库,以便使用它们提供的功能。代码中的 import 语句就是用来实现这一功能的。 eslint-plugin-organize-imports 通过对 import 语句的静态分析,提供了一些自动化的代码组织和规范化功能,以保持代码的整洁性和一致性。 该插件可以帮助我们实现以下功能: 1. 自动按照字母顺序对 import 语句进行排序。这样做可以提高代码可读性,减少重复的 import 语句,避免遗漏某些模块。 2. 自动移除未使用的 import 语句。当项目逐渐变大时,可能会出现一些无用的 import 语句,通过该插件可以自动删除这些未使用的 import 语句,提高代码的执行效率。 3. 自动分组 import 语句。当引入的模块来自不同的来源时,可以将它们按照不同的分组进行组织,以提高代码的可读性和维护性。 4. 自定义配置选项。该插件提供了丰富的配置选项,可以根据项目的需求,对不同的组织规则进行灵活配置。 借助 eslint-plugin-organize-imports,我们可以轻松地优化 JavaScript 代码的 import 语句,使其更加整齐规范,提高代码的可读性和可维护性。这对于大型项目和团队合作开发尤其重要。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值