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. cocos2d源码剖析

    1. TextureAtlas http://www.cocoachina.com/bbs/read.php?tid-311439-keyword-TextureAtlas.html 2. Label ...

  2. iOS 高仿:花田小憩3.0.1

    前言 断断续续的已经学习Swift一年多了, 从1.2到现在的2.2, 一直在语法之间徘徊, 学一段时间, 工作一忙, 再捡起来隔段时间又忘了.思来想去, 趁着这两个月加班不是特别多, 就决定用swi ...

  3. 隐藏iframe边框

    关于iframe在ie浏览器中边框隐藏的问题,一直困惑着我.其实就是一个很简单的办法,主要设置一个属性即可解决,方法如下: <iframe frameborder="0"&g ...

  4. objective c 学习(一)

    问题一:我在程序中看到大量的减号.中括号和NS****这种东西,他们是什么玩意儿? 1 减号(或者加号) 减号表示一个函数.或者方法.或者消息的开始,怎么说都行. 比如c#中,一个方法的写法可能是: ...

  5. Block之变量作用域

    在使用block的过程中经常会调用不同类型.不同作用域的变量,如果对这些变量作用域的理解稍有偏差,就会出现问题.故此特意整理出block中会经常使用到的几种变量,如有补充,欢迎指出. 1. 局部变量 ...

  6. 咱也玩玩Wordpress

    博客暂时转移到了 ->  www.zhyfzy.ga 域名改成.com啦 -> www.zhyfzy.com

  7. 9.23 noip模拟试题

      Problem 1 抓牛(catchcow.cpp/c/pas) [题目描述] 农夫约翰被通知,他的一只奶牛逃逸了!所以他决定,马上出发,尽快把那只奶牛抓回来. 他们都站在数轴上.约翰在N(O≤N ...

  8. webservice 发布到外网的时候

    在web.config的<system.web></system.web>中间加入如下配置节内容<webServices>          <protoco ...

  9. VB中右键换行

    /r/n  能在邮件中进行换行, 在VB中使用 ASCII码的 chr(10).chr(13) 就能使VB发送邮件实现换行

  10. memcached和mongodb 在windows下安装

    要在新机器上安装memcached和mongodb服务,折腾了一天,终于把这两个服务在windows下跑起来了. memcached主要参考http://www.rootop.org/pages/27 ...