POJ 3007 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 (字典树 静态)的更多相关文章
- POJ 3007 Organize Your Train part II
题意: 如上图所示,将一个字符串进行分割,反转等操作后不同字符串的个数: 例如字符串abba:可以按三种比例分割:1:3:2:2:3:1 部分反转可以得到如下所有的字符串: 去掉重复可以得到六个不同的 ...
- Organize Your Train part II 字典树(此题专卡STL)
Organize Your Train part II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8787 Acce ...
- poj 3007 Organize Your Train part II(静态字典树哈希)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6700 Accepted: 1922 Description RJ Freigh ...
- poj 3007 Organize Your Train part II(二叉排序树)
题目:http://poj.org/problem?id=3007 题意:按照图示的改变字符串,问有多少种..字符串.. 思路:分几种排序的方法,,刚开始用map 超时(map效率不高啊..),后来搜 ...
- POJ 3007 Organize Your Train part II(哈希链地址法)
http://poj.org/problem?id=3007 题意 :给你一个字符串,让你无论从什么地方分割,把这个字符串分成两部分s1和s2,然后再求出s3和s4,让你进行组合,看能出来多少种不同的 ...
- POJ 3007:Organize Your Train part II
Organize Your Train part II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7561 Acce ...
- poj 2503 Babelfish(Map、Hash、字典树)
题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码( ...
- ACM学习历程—POJ 3764 The xor-longest Path(xor && 字典树 && 贪心)
题目链接:http://poj.org/problem?id=3764 题目大意是在树上求一条路径,使得xor和最大. 由于是在树上,所以两个结点之间应有唯一路径. 而xor(u, v) = xor( ...
- nyoj 230/poj 2513 彩色棒 并查集+字典树+欧拉回路
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=230 题意:给你许许多多的木棍,没条木棍两端有两种颜色,问你在将木棍相连时,接触的端点颜色 ...
随机推荐
- Informatica 常用组件Source Qualifier之九 创建SQ转换
可以配置 Designer 在您将源拖到映射中时默认创建源限定符转换,您也可以手动创建源限定符转换. 默认创建源限定符转换 可以配置 Designer 在您将源拖到映射中时自动创建一个源限定符转换. ...
- Longest Valid Parentheses leetcode java
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- 超级简单的jquery操作表格(添加/删除行、添加/删除列)
利用jquery给指定的table添加一行.删除一行 <script language="javascript" src="./jquery.js"> ...
- [Algorithm] Search for matching words
Implement an autocomplete system. That is, given a query string s and a set of all possible query st ...
- 高并发分布式环境中获取全局唯一ID[分布式数据库全局唯一主键生成]
需求说明 在过去单机系统中,生成唯一ID比较简单,可以使用MySQL的自增主键或者Oracle中的sequence, 在现在的大型高并发分布式系统中,以上策略就会有问题了,因为不同的数据库会部署到不同 ...
- ZH奶酪:标准偏差
标准偏差 标准偏差(Std Dev,Standard Deviation) -统计学名词.一种量度数据分布的分散程度之标准,用以衡量数据值偏离算术平均值的程度.标准偏差越小,这些值偏离平均值就越少,反 ...
- Android Developers:日历提供者
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="ht ...
- 10个最受欢迎的Java类(转)
原文:http://www.csdn.net/article/2012-06-04/2806277 每一个Java程序员都有一份属于自己的Java类排名表.这个排名表没有严格的规定,也没有可遵循的规则 ...
- 【React Native开发】React Native控件之ListView组件解说以及最齐全实例(19)
),React Native技术交流4群(458982758).请不要反复加群!欢迎各位大牛,React Native技术爱好者加入交流!同一时候博客左側欢迎微信扫描关注订阅号,移动技术干货,精彩文章 ...
- 检查用户输入信息是否完整(vb.net实现)
机房收费系统中.在将用户输入的信息封装到实体中作为參数传到B层之前,总要对用户输入的信息进行检查.我将这种检查分为两类: 合法性检查 完整性检查 所谓合法性检查,就是用户输入的信息是否 ...