poj 3007 Organize Your Train part II(静态字典树哈希)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 6700 | Accepted: 1922 |
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
题意很简单,把一个字符串拆成两部分,每个字符串都可以逆置也可以两部分自由组合,这样就有8种组合方式,问不重复的字符串有几个;
原本很水的一个题,最后被我做的面目全非。一开始一直MLE,我就写了个DELETE函数将内存释放掉;接着就一直TLE到死,最后看了别人的解题报告说字典树是用空间换时间,是不会TLE的,原因在于malloc函数重复使用,所以就申请一个静态的结构体数组,这样才终于A了;

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std; struct Tree
{
bool flag;
struct Tree *next[];
}mem[];
int size; struct Tree *new_tree()
{
return mem+(size++);
} void insert(struct Tree *tree,char s[])
{
int i,t;
struct Tree *p = tree;
for(i = ; s[i]; i++)
{
t = s[i]-'a';
if(p->next[t] == NULL)
{
Tree *q = new_tree();
q->flag = ;
memset(q->next,,sizeof(q->next));
p->next[t] = q;
p = q;
}
else
p = p->next[t];
}
p->flag = ;
} int search(struct Tree *tree, char s[])
{
struct Tree *p = tree;
int t,i;
for(i = ; s[i]; i++)
{
t = s[i]-'a';
if(p->next[t] == NULL)
{
insert(tree,s);
return ;
}
p = p->next[t];
}
if(p->flag)
return ;
insert(tree,s);
return ;
}
int main()
{
int item,i,j,len;
char s[];
char str[]; scanf("%d",&item);
while(item--)
{
size = ;
struct Tree *tree;
int cnt = ;
scanf("%s",s);
len = strlen(s); tree = new_tree();
for(i = ; i < ; i++)
tree->next[i] = NULL; insert(tree,s);
for(i = ; i < len; i++)
{
for(j = ; j < len; j++)//+1-2
{
if(j < i) str[j] = s[j];
else str[j] = s[len--j+i];
}
str[len] = '\0';
cnt += search(tree,str); for(j = ; j < len; j++)//-1+2
{
if(j < i) str[j] = s[i--j];
else str[j] = s[j];
}
str[len] = '\0';
cnt += search(tree,str); for(j = ; j < len; j++)//-1-2
{
if(j < i) str[j] = s[i--j];
else str[j] = s[len--j+i];
}
str[len] = '\0';
cnt += search(tree,str); for(j = ; j < len; j++)//+2+1
{
if(j < len-i) str[j] = s[i+j];
else str[j] = s[j+i-len];
}
str[len] = '\0';
cnt += search(tree,str); for(j = ; j < len; j++)//+2-1
{
if(j < len-i) str[j] = s[i+j];
else str[j] = s[len-j-];
}
str[len] = '\0';
cnt += search(tree,str); for(j = ; j < len; j++)//-2+1
{
if(j < len-i) str[j] = s[len--j];
else str[j] = s[j+i-len];
}
str[len] = '\0';
cnt += search(tree,str); for(j = ; j < len; j++)//-2-1
{
if(j < len-i) str[j] = s[len--j];
else str[j] = s[len--j];
}
str[len] = '\0';
cnt += search(tree,str);
}
printf("%d\n",cnt);
}
return ;
}
poj 3007 Organize Your Train part II(静态字典树哈希)的更多相关文章
- POJ 3007 Organize Your Train part II (字典树 静态)
Organize Your Train part II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6478 Acce ...
- POJ 3007 Organize Your Train part II
题意: 如上图所示,将一个字符串进行分割,反转等操作后不同字符串的个数: 例如字符串abba:可以按三种比例分割:1:3:2:2:3:1 部分反转可以得到如下所有的字符串: 去掉重复可以得到六个不同的 ...
- 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 ...
- Organize Your Train part II 字典树(此题专卡STL)
Organize Your Train part II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8787 Acce ...
- nyoj 163 Phone List(动态字典树<trie>) poj Phone List (静态字典树<trie>)
Phone List 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Given a list of phone numbers, determine if it i ...
- POJ 2513 Colored Sticks(欧拉道路+字典树+并查集)
http://poj.org/problem?id=2513 题意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 思路: 题目很明 ...
- poj Organize Your Train part II
http://poj.org/problem?id=3007 #include<cstdio> #include<algorithm> #include<cstring& ...
随机推荐
- IOS-CGAffineTransformMake 矩阵变换 的运算原理
1.矩阵的基本知识: struct CGAffineTransform { CGFloat a, b, c, d; CGFloat tx, ty; }; CGAffineTransform C ...
- ios中图片的绘画和截图
ios中图片的绘画和截图 CGImageCreateWithImageInRect截图和UIGraphicsGetImageFromCurrentImageContext绘画图片 使用CGImageC ...
- 微信公众号支付流程(Node实现)
前言 花费了一天时间,调通了微信公众号支付.作下记录,方便以后再次填坑.先声明,微信公众号支付,不同于微信H5支付,这点在本文结束时再详细说明. 微信配置 设置测试目录 在微信公众平台设置,栏目见下图 ...
- 本地代码git到github上
本地代码git到github上 对于个程序员来说,不写自己的博客,没有自己的作品集没有Github就不算好的程序员!咳咳~ 开个玩笑.对于我个人来说,要做个程序员,就要有自己的作品集和技术博客(我说是 ...
- CSS盒子模型小剖析
前段时间刚刚从C/S过度到B/S,提到B/S就不能说CSS,而说起CSS又不能落下盒子模型.在CSS诞生的时候就有了盒子模型的概念,网页中大部分的元素都能构成一个盒子模型,.盒子模型无非就是描述的元素 ...
- 高性能动画!HTML5 Canvas JavaScript框架KineticJS
高性能动画!HTML5 Canvas JavaScript框架KineticJS KineticJS是一款开源的HTML5 Canvas JavaScript框架,能为桌面和移动应用提供高性能动画,并 ...
- 重载,重写和super
1.重载的概念:----->在同一个类中,允许存在同名函数,但它们的参数个数或者参数类型不同即可.public static void main(String[] args){System.ou ...
- 使用OC开发phonegp 组件
使用OC开发phonegp 组件 1. 使用oc 对phonegp中的组件近些开发,首先具体的pgonegp跟nativecode之间的一些优劣就不说了,开发phonegp 对应的组件主要就是使用na ...
- IOS 生成设备唯一标识
前言 iOS设备5.0以上放弃使用[[UIDevice currentDevice] uniqueIdentifier]来获得设备唯一ID iOS设备私有方法禁止用户获取和使用IMEI 需求 需要一个 ...
- mysqldump备份、还原数据库路径名含有空格的处理方法(如:Program Files)
虽然以下的方法也可以解决,不过最简单直接的,还是直接在路径前后加双引号-" ",这个方法简单有效. 首先要说明的是mysqldump.exe在哪里不重要,重要的是要处理好路径中的非 ...