Organize Your Train part II
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6478   Accepted: 1871

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):

  1. [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

  1. 4
  2. aa
  3. abba
  4. abcd
  5. abcde

Sample Output

  1. 1
  2. 6
  3. 12
  4. 18

Source

 
  1. 题目大意:就是一个字符串拆成两部分,然后任何一个可以旋转,然后两个的位置可以变动,所以总共有8种组合,问总共能拼成多少个不同的字符串。
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4.  
  5. using namespace std;
  6.  
  7. struct Trie{
  8. int cnt;
  9. int next[];
  10. }root[];
  11.  
  12. char str[];
  13. int len,top,ans;
  14.  
  15. void init(int k){
  16. for(int i=;i<;i++)
  17. root[k].next[i]=-;
  18. root[k].cnt=;
  19. }
  20.  
  21. void InsertTrie(int p){
  22. for(int i=;i<len;i++){
  23. int id=str[i]-'a';
  24. if(root[p].next[id]==-){
  25. root[p].next[id]=top;
  26. init(top);
  27. top++;
  28. }
  29. p=root[p].next[id];
  30. }
  31. if(root[p].cnt==){
  32. ans++;
  33. root[p].cnt++;
  34. }
  35. }
  36.  
  37. int main(){
  38.  
  39. //freopen("input.txt","r",stdin);
  40.  
  41. char s1[],s2[],s3[],s4[],s5[];
  42. int head,t;
  43. scanf("%d",&t);
  44. while(t--){
  45. scanf("%s",s1);
  46. len=strlen(s1);
  47. ans=;
  48. head=;
  49. top=;
  50. init(head);
  51. int i,j;
  52. for(i=;i<len;i++){
  53. for(j=;j<i;j++){
  54. s2[j]=s1[j];
  55. s4[i--j]=s2[j];
  56. }
  57. s2[i]='\0'; s4[i]='\0';
  58. for(j=i;j<len;j++){
  59. s3[j-i]=s1[j];
  60. s5[len--j]=s3[j-i];
  61. }
  62. s3[j-i]='\0'; s5[j-i]='\0';
  63. strcpy(str,s2); strcat(str,s3);
  64. InsertTrie(head);
  65.  
  66. strcpy(str,s2); strcat(str,s5);
  67. InsertTrie(head);
  68.  
  69. strcpy(str,s3); strcat(str,s2);
  70. InsertTrie(head);
  71.  
  72. strcpy(str,s3); strcat(str,s4);
  73. InsertTrie(head);
  74.  
  75. strcpy(str,s4); strcat(str,s3);
  76. InsertTrie(head);
  77.  
  78. strcpy(str,s4); strcat(str,s5);
  79. InsertTrie(head);
  80.  
  81. strcpy(str,s5); strcat(str,s2);
  82. InsertTrie(head);
  83.  
  84. strcpy(str,s5); strcat(str,s4);
  85. InsertTrie(head);
  86. }
  87. printf("%d\n",ans);
  88. }
  89. return ;
  90. }

POJ 3007 Organize Your Train part II (字典树 静态)的更多相关文章

  1. POJ 3007 Organize Your Train part II

    题意: 如上图所示,将一个字符串进行分割,反转等操作后不同字符串的个数: 例如字符串abba:可以按三种比例分割:1:3:2:2:3:1 部分反转可以得到如下所有的字符串: 去掉重复可以得到六个不同的 ...

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

    Organize Your Train part II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8787   Acce ...

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

    Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6700 Accepted: 1922 Description RJ Freigh ...

  4. poj 3007 Organize Your Train part II(二叉排序树)

    题目:http://poj.org/problem?id=3007 题意:按照图示的改变字符串,问有多少种..字符串.. 思路:分几种排序的方法,,刚开始用map 超时(map效率不高啊..),后来搜 ...

  5. POJ 3007 Organize Your Train part II(哈希链地址法)

    http://poj.org/problem?id=3007 题意 :给你一个字符串,让你无论从什么地方分割,把这个字符串分成两部分s1和s2,然后再求出s3和s4,让你进行组合,看能出来多少种不同的 ...

  6. POJ 3007:Organize Your Train part II

    Organize Your Train part II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7561   Acce ...

  7. poj 2503 Babelfish(Map、Hash、字典树)

    题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码( ...

  8. ACM学习历程—POJ 3764 The xor-longest Path(xor && 字典树 && 贪心)

    题目链接:http://poj.org/problem?id=3764 题目大意是在树上求一条路径,使得xor和最大. 由于是在树上,所以两个结点之间应有唯一路径. 而xor(u, v) = xor( ...

  9. nyoj 230/poj 2513 彩色棒 并查集+字典树+欧拉回路

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=230 题意:给你许许多多的木棍,没条木棍两端有两种颜色,问你在将木棍相连时,接触的端点颜色 ...

随机推荐

  1. 版本号控制-git(二)

    上次文章给大家介绍了Git的一些基本知识(http://www.cnblogs.com/jerehedu/p/4582398.html).并介绍了使用git init初始化化版本号库.使用git ad ...

  2. Mysql之sql语句操作

    一.数据库级别操作 1.显示数据库 1 SHOW DATABASES; 默认数据库: mysql - 用户权限相关数据 test - 用于用户测试数据 information_schema - MyS ...

  3. asp.net中使用ueditor 1.3.6上传图片问题

    在asp.net中使用ueditor 1.3.6版本上传图片时,出现上传成功但是,图片无法正常显示的问题,解决方法如下: 只需要将imageUp.ashx中的info = up.upFile(cont ...

  4. Mac OS中Java Servlet与Http通信

    Java中一个Servlet其实就是一个类,用来扩展服务器的性能,服务器上驻留着可以通过“请求-响应”编程模型来访问的应用程序.Servlet可以对任何类型的请求产生响应,但通常只用来扩展Web服务器 ...

  5. 在C#中使用属性控制 XML 序列化来解析XML

    今天需要解析一个XML,这个XML和一般情况用.NET的序列化出来的格式不太一样. 我就又补习了一下. 分享一下学习成果吧. 示例代码下载: http://download.csdn.net/deta ...

  6. Clion安装配置

    1.安装配置MinGW MinGW安装和使用 Windows下利用Cygwin搭建C/C++开发环境GCC 注:分别是使用MinGW和Cygwin两种方法安装GCC环境,据说Cygwin会好一点……我 ...

  7. 全局 Style

    1.定义一个全局资源文件,如下 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/pres ...

  8. iOS_2_button控制物体形变

    终于效果图: BeyondViewController.h // // BeyondViewController.h // 02_button控制物体形变 // // Created by beyon ...

  9. Android基础新手教程——1.6 .9(九妹)图片怎么玩

    Android基础新手教程--1.6 .9(九妹)图片怎么玩 标签(空格分隔): Android基础新手教程 1.本节引言: 可能有的一些疑问: 1.什么是.9图片? 答:图片后缀名前有.9的图片,如 ...

  10. [Algorithm] Largest sum of non-adjacent numbers

    Given a list of integers, write a function that returns the largest sum of non-adjacent numbers. Num ...