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

  [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

 
题目大意:就是一个字符串拆成两部分,然后任何一个可以旋转,然后两个的位置可以变动,所以总共有8种组合,问总共能拼成多少个不同的字符串。
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; struct Trie{
int cnt;
int next[];
}root[]; char str[];
int len,top,ans; void init(int k){
for(int i=;i<;i++)
root[k].next[i]=-;
root[k].cnt=;
} void InsertTrie(int p){
for(int i=;i<len;i++){
int id=str[i]-'a';
if(root[p].next[id]==-){
root[p].next[id]=top;
init(top);
top++;
}
p=root[p].next[id];
}
if(root[p].cnt==){
ans++;
root[p].cnt++;
}
} int main(){ //freopen("input.txt","r",stdin); char s1[],s2[],s3[],s4[],s5[];
int head,t;
scanf("%d",&t);
while(t--){
scanf("%s",s1);
len=strlen(s1);
ans=;
head=;
top=;
init(head);
int i,j;
for(i=;i<len;i++){
for(j=;j<i;j++){
s2[j]=s1[j];
s4[i--j]=s2[j];
}
s2[i]='\0'; s4[i]='\0';
for(j=i;j<len;j++){
s3[j-i]=s1[j];
s5[len--j]=s3[j-i];
}
s3[j-i]='\0'; s5[j-i]='\0';
strcpy(str,s2); strcat(str,s3);
InsertTrie(head); strcpy(str,s2); strcat(str,s5);
InsertTrie(head); strcpy(str,s3); strcat(str,s2);
InsertTrie(head); strcpy(str,s3); strcat(str,s4);
InsertTrie(head); strcpy(str,s4); strcat(str,s3);
InsertTrie(head); strcpy(str,s4); strcat(str,s5);
InsertTrie(head); strcpy(str,s5); strcat(str,s2);
InsertTrie(head); strcpy(str,s5); strcat(str,s4);
InsertTrie(head);
}
printf("%d\n",ans);
}
return ;
}

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. 为apache安装mod_wsgi的时候出现-fpic的问题

    1.为了在apache里跑python项目,需要安装模块mod_wsgi 2.但是由于yum只支持python2.6,所以通过yum install mod_wsgi方式安装的mod_wsgi是pyt ...

  2. java使用省略号代替多参数(参数类型... 参数名)

    J2SE 1.5提供了“Varargs”机制.借助这一机制,可以定义能和多个实参相匹配的形参.从而,可以用一种更简单的方式,来传递个数可变的实参.本文介绍这一机制的使用方法,以及这一机制与数组.泛型. ...

  3. Sort List leetcode java

    题目: Sort a linked list in O(n log n) time using constant space complexity. 题解: 考虑到要求用O(nlogn)的时间复杂度和 ...

  4. js判断是否为手机访问

    JavaScript判断浏览器类型一般有两种办法,一种是根据各种浏览器独有的属性来分辨,另一种是通过分析浏览器的userAgent属性来判断的.在许多情况下,值判断出浏览器类型之后,还需判断浏览器版本 ...

  5. SQL中以count或sum为条件的查询方式

    在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询.比如user_num表: id user num 1 a 3 2 a 4 3 b 5 4 b 7   例1:查询出现 ...

  6. IOS之导航控制器传值

    UITableView有两种风格:UITableViewStylePlain和UITableViewStyleGrouped.这两者操作起来其实并没有本质区别,只是后者按分组样式显示前者按照普通样式显 ...

  7. PL/SQL 下 Command window 与 SQL window 的区别

    1.Command window实现了SQL*Plus的所有功能,允许运行sql*plus命令,sql命令,sql脚本. 2.SQL window用于执行sql语句,显示sql输出,执行统计信息.(测 ...

  8. 微博推荐算法学习(Weibo Recommend Algolrithm)

    原文:http://hijiangtao.github.io/2014/10/06/WeiboRecommendAlgorithm/ 基础及关联算法 作用:为微博推荐挖掘必要的基础资源.解决推荐时的通 ...

  9. 关于bootstrap-datetimepicker.js不支持IE8的解决办法

    if (!Array.prototype.indexOf) Array.prototype.indexOf = function (elt /*, from*/) { var len = this.l ...

  10. Jquery 应用积累

    1.控制div显隐 $("#id").show()表示display:block, $("#id").hide()表示display:none; $(" ...