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& ...
随机推荐
- 基于HTML5的SLG游戏开发(一):搭建开发环境(1)
开发环境: 1.操作系统:MacOS 10.8.5 2.本地web服务器:Apache 2.2.24 (Window环境推荐使用WampServer) 3.编码工具:WebStrom 7.0 4.调试 ...
- 一个很简单的SqlServer生成常用C#语句工具的诞生
前言: 这个文章只要是记录一下,这个工具的诞生过程.作用.其中的技术实在是太简单可以说没有什么技术~主要是锻炼一下写文章的能力! 正文: 在开发项目的时,常常会要维护或变更一些老项目,涉及到简单的几张 ...
- 使用PowerShell读、写、删除注册表键值
访问注册表键值 在PowerShell中,用户可以通过类似于HKCU:(作为HKEY_CURRENT_USER)和HKLM:(代表HKEY_LOCAL_MATCHINE)的虚拟驱动器访问注册表键值. ...
- Oracle11g数据库导入到oracle10g的解决方法
我想有很多人在工作和学习中遇到这样的一个问题,Oracle数据库服务器版本和本机版本不一致问题,你的本机要是比服务器的版本要高的话还好,如果你本机是10g服务器是11g的话,从11g导出来的数据库是导 ...
- CoreBluetooth
Core Bluetooth的基本常识 每个蓝牙4.0设备都是通过服务(Service)和特征(Characteristic)来展示自己的 一个设备必然包含一个或多个服务,每个服务下面又包含若干个特征 ...
- 一条sql语句循环插入N条不同记录(转)
SET NOCOUNT ON IF (OBJECT_ID('TB' ) IS NOT NULL ) DROP TABLE TB GO CREATE TABLE TB(ID INT IDENTITY ( ...
- css 不确定元素宽度的水平居中
对于一个不确定宽度的元素居中,我们想到使用的方法是 text-align:center; 或者 margin:0 auto; text-align只对行内元素有效,对于块元素我们要用margin,块元 ...
- ScheduleThreadPoolExecutor源码分析(二)
DelayedWorkQueue: DelayedWorkQueue实现了BlockingQueue接口,因此其可以作为线程池的任务队列.BlockingQueue的主要属性有以下几个: privat ...
- 不使用ASP.NET服务器端控件(包括form表单不加runat="server")来触发.cs里的事件(方法),(适用于有代码洁癖者)。
很多时候,我们使用服务器端控件写出的代码,会给我们生成一些很多我们看不懂的代码(初学者),但是有时候我们并不需要这些代码(业务需求不同),对于生成的一些代码感到多余.所以我就开始想,有没有一种可能:不 ...
- C# 窗体靠近屏幕边缘自动隐藏*学习(类似于QQ)
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; us ...