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 题意:给你许许多多的木棍,没条木棍两端有两种颜色,问你在将木棍相连时,接触的端点颜色 ...
随机推荐
- mysqldump与source
mysqldump示例 mysqldump --default-character-set=utf8 -d --opt -hlocalhost -uroot -p123456 --where=&quo ...
- Informatica 常用组件Lookup缓存之三 重建查找高速缓存
如果您认为查找源在 PowerCenter 上次构建高速缓存时已更改,则可指示 PowerCenter 重建查找高速缓存. 重建高速缓存时,PowerCenter 会覆盖现有永久高速缓存文件而创建新的 ...
- [Vue warn]: Error in render: "TypeError: Cannot read property '0' of undefined、vuejs路由使用的问题Error in render function
1.[Vue warn]: Error in render: "TypeError: Cannot read property '0' of undefined 注意,只要出现Error i ...
- 设计模式之十五:訪问者模式(Visitor Pattern)
訪问者模式(Visitor Pattern)是GoF提出的23种设计模式中的一种,属于行为模式. 据<大话设计模式>中说算是最复杂也是最难以理解的一种模式了. 定义(源于GoF<De ...
- zypper命令使用示例
导读 Zypper是OpenSUSE和企业版SUSE中软件包管理器ZYpp的命令行接口. 主要用于:1.管理软件包:zypper可用来安装.删除.更新和查询本地或远程的软件包.2.管理仓库:zyppe ...
- 学习技巧-如何在IBM官网寻找学习资料
场景:最近看招聘职位TM1比较火,于是就想找一下Cognos TM1的资料来拜读一下,然后论坛都是大价钱的金币,迫于无奈只好来到IBM的官网来寻求指导 http://www.ibm.com/us/en ...
- 如何解决SPD的缓存问题
SPD有时候文件被缓存住了,表现为文件的最后更改时间不对,或者本来文件已经被check in了,但是显示check out状态,而此时如果选择check in, 就会提示文件没有被check ou ...
- 翻译记忆软件-塔多思TRADO经典教程_2
Trados 中文简明教程Trados 中文简明教程1. 准备工作 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...
- VM虚拟机如何和主机共享文件夹或文件
请一定要选中Map as a network drive in Windows guests,否则将无法查看共享.
- Office WORD如何关闭自动检查语法
只要把打钩的地方全部去掉即可.