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 题意:给你许许多多的木棍,没条木棍两端有两种颜色,问你在将木棍相连时,接触的端点颜色 ...
随机推荐
- Installing Apache Spark on Ubuntu 16.04
Santosh Srinivas on 07 Nov 2016, tagged onApache Spark, Analytics, Data Minin I've finally got to a ...
- 用wubi安装的Ubuntu在重装Windows 7系统后,如何恢复(转)
原文链接:双系统Win7+Ubuntu,重装Win7后找不到Ubuntu启动引导项问题 1.把安装ubuntu->winboot文件夹下wubidr和wubidr.mbr两个文件拷到C盘根目录下 ...
- Objective-C:OC内部可变对象和不可变对象的深(复制)拷贝问题思考:
OC内部:可变对象和不可变对象的深(复制)拷贝问题思考: 不可变对象: 例如NSString对象,因为NSString对象是常量字符串,所以,不可以更改其内容,但是可以修改指向该字符串的指针指向 ...
- 附3 springboot源码解析 - 构建SpringApplication
package com.microservice.framework; import org.springframework.boot.SpringApplication; import org.sp ...
- :Windows下RabbitMQ安装及入门
1.Windows下安装RabbitMQ需要以下几个步骤 (1):下载erlang,原因在于RabbitMQ服务端代码是使用并发式语言erlang编写的,下载地址:http://www.erlang. ...
- JDBC上关于数据库中多表操作一对多关系和多对多关系的实现方法
黑马程序员 我们知道,在设计一个Javabean的时候,要把这些BEAN 的数据存放在数据库中的表结构,然而这些数据库中的表直接又有些特殊的关系,例如员工与部门直接有一对多的关系,学生与老师直接又多对 ...
- 按示例学python:使用python抓取网页正文
平时打开一个网页,除了文章的正文内容,通常会有一大堆的导航,广告和其他方面的信息.本博客的目的,在于说明如何从一个网页中提取出文章的正文内容,而过渡掉其他无关的的信息. 这里先看看 demo : ht ...
- Postgresql监控pgwatch的搭建
一,需要环境: You will need a handful of components to make this work: - Apache (webserver) #apache搭建web页面 ...
- linux内核——进程管理
在讲进程之前先说一下进程的堆栈的吧: 1.进程的堆栈 内核在创建进程的时候,在创建task_struct的同一时候,会为进程创建对应的堆栈.每一个进程会有两个栈,一个用户栈.存在于用户空间,一个内核栈 ...
- Memcached--分布式缓存安装教程
Memcached的Windows版本在这里下载http://code.google.com/p/memcached/wiki/PlatformWindows(或http://memcachedpro ...