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(静态字典树哈希)的更多相关文章

  1. POJ 3007 Organize Your Train part II (字典树 静态)

    Organize Your Train part II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6478   Acce ...

  2. POJ 3007 Organize Your Train part II

    题意: 如上图所示,将一个字符串进行分割,反转等操作后不同字符串的个数: 例如字符串abba:可以按三种比例分割:1:3:2:2:3:1 部分反转可以得到如下所有的字符串: 去掉重复可以得到六个不同的 ...

  3. poj 3007 Organize Your Train part II(二叉排序树)

    题目:http://poj.org/problem?id=3007 题意:按照图示的改变字符串,问有多少种..字符串.. 思路:分几种排序的方法,,刚开始用map 超时(map效率不高啊..),后来搜 ...

  4. POJ 3007 Organize Your Train part II(哈希链地址法)

    http://poj.org/problem?id=3007 题意 :给你一个字符串,让你无论从什么地方分割,把这个字符串分成两部分s1和s2,然后再求出s3和s4,让你进行组合,看能出来多少种不同的 ...

  5. POJ 3007:Organize Your Train part II

    Organize Your Train part II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7561   Acce ...

  6. Organize Your Train part II 字典树(此题专卡STL)

    Organize Your Train part II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8787   Acce ...

  7. 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 ...

  8. POJ 2513 Colored Sticks(欧拉道路+字典树+并查集)

    http://poj.org/problem?id=2513 题意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 思路: 题目很明 ...

  9. poj Organize Your Train part II

    http://poj.org/problem?id=3007 #include<cstdio> #include<algorithm> #include<cstring& ...

随机推荐

  1. eclipse 常见问题及解决

    1. Target runtime Apache Tomcat v6.0 is not defined.错误解决方法 原文:http://blog.csdn.net/xw13106209/articl ...

  2. 了解ANSI编码

    ANSI:American National Standards Institute:美国国家标准学会 ANSI编码:为使计算机支持更多语言,不同国家和地区分别制定了符合自身的外文字符延伸编码方式(如 ...

  3. iOS UIKit:viewController之Present (3)

    弹出和转换view controller技术是一种快速且简单的方式将新view content展示在屏幕中.目前有两种方式弹出新的view controller:Present方式和segues方式. ...

  4. 第五篇:python高级之面向对象高级

    python高级之面向对象高级   python高级之面向对象高级 本节内容 成员修饰符 特殊成员 类与对象 异常处理 反射/自省 单例模式 1.成员修饰符 python的类中只有私有成员和公有成员两 ...

  5. Android 控件 之 Menu 菜单

    http://www.cnblogs.com/Mrs-cc/archive/2012/07/21/2603042.html 1.OptionsMenu (选项菜单)用法总结   使用方法: 方法一:添 ...

  6. .net 使用AjaxControlToolkit.dll 遇到的"Sys"未定义问题

    1.配置文件一般都会缺少<httpHandlers></httpHandlers> 这一段, <httpHandlers> <remove verb=&quo ...

  7. app图标和启动页设置

    弄了一下午,终于把iOS中图标的设置和启动页的设置弄明白了.我想以后再也不会浑了. 进入正题: 一:apple 1).iPhone4s 3.5寸屏,也就是640*960,但在模拟器上正常用的是320* ...

  8. 九、C# 合式类型

    本章要描述如何最终完善类型声明.   1.重写Ojbect中的成员   重写ToString() 默认情况下,在任何对象上调用 ToString()会返回类的完全限定名称,所以有时候需要重载这个函数, ...

  9. innerHTML/outerHTML; innerText/outerText; textContent

    innerHTML v.s. outerHTML Element.innerHTML Reference: https://developer.mozilla.org/en-US/docs/Web/A ...

  10. 内容替换Filter

    有时候需要对网站进行控制,防止输出非法内容或者敏感信息.这时我们可以使用filter来进行内容替换,其工作原理为,在Servlet将内容输出到response时,response将内容缓存起来,在Fi ...