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. xslt语法之---position()函数

    最近在学习使用XSLT,很好很强大的样式表语言.使用到了position()函数特此记录一下. position()函数--返回节点位置 语法:position() 参数:无 返回值:整数 用途:该函 ...

  2. CentOS使用sendmail发送邮件

    1.安装sendmail yum -y install sendmail 2.启动sendmail服务 service sendmail start 3.将发件内容写入mail.txt mail -s ...

  3. 前端自动化构建工具 Gulp 使用

    一个月没写博客了,今天有时间,就写个gulp的入门使用吧.. 简介:gulp是一个前端自动化构建工具,可以实现代码的检查.压缩.合并……等等,gulp是基于Node.js的自动任务运行器 一.安装No ...

  4. Length 和 Width在矩形中的定义.

    Length is the longer or longest dimension of a rectangle (or even an object). Ref:http://mathforum.o ...

  5. php100视频原始地址列表整理:

    php100视频原始地址列表整理: 教程名称 . 1:环境配置与代码调试 2:PHP的数据类型与源码调试 3:常用PHP运算类型介绍与应用 4: PHP条件语句介绍与应用 5:PHP循环语句的介绍与应 ...

  6. mysql 远程访问 配置

    sudo vi /etc/mysql/my.cnf 找到bind-address = 127.0.0.1 注释掉这行:#bind-address = 127.0.0.1 或者改为: bind-addr ...

  7. sql查询过程中 update,insert,delete可视化收影响行数

    insert into test_tb output inserted.id,inserted.data values('c'),('d') delete from test_tb output de ...

  8. Spot light工具集

    Spot light on UNIX 安装没什么问题 Spot light on Oracle  必须安装32位的客户端,不然搞死你 两者的界面都是吊炸天啊

  9. 段落排版--行间距, 行高(line-height)

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  10. [转]mysql 导入导出数据库以及函数、存储过程的介绍

    本篇文章是对mysql中的导入导出数据库命令以及函数.存储过程进行了详细的分析介绍,需要的朋友参考下: mysql常用导出数据命令:1.mysql导出整个数据库  mysqldump -hhostna ...