Language: Default日本語
Organize Your Train part II
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8086   Accepted: 2339

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

Source

Japan 2006 Domestic
Analysis
题目大意:给一个只包含小写字母的字符串数组,从任意位置分割,互相反转组合,问除去重复后的最多组合数。因为字符串的长度最大是72,所以直接暴力分割计数即可,用静态字典树记录出现过的状态。
不得不说,这题还真是坑,开始做的时候,看了半天不会做,于是去找题解,看到网上大神们有的用哈希,有的用平衡二叉树记录状态,于是想到用字典树做,代码量和思维都要简单不少,但是。。。
第一次MLE是因为我用的动态字典树,要申请大量的空间,于是我在每组结束时回收内存,于是就来了第二次的TLE,第三次和第四次我不回收内存了,直接初始化第一次申请的空间,第三次初始化的地方写错了,还是MLE,第四次写是写对了,又TLE了,可想而知我的内心是崩溃的,到这时候终于知道是用静态字典树做了,开了个100w的结构体数组,不知为啥还是MLE啊啊啊啊啊,我又删掉了一个0变成10w才过的,但是我看到网上别人做就是开的100w啊?????Why!!!!!
Source Code
#include <cstdio>
#include <cstring>
using namespace std;
struct node
{int flag,next[26];
} p[100086];
int ans,top;
void create(int x)
{p[x].flag=0;for (int i=0; i<26; i++)p[x].next[i]=-1;
}
void add(int x,char *s,int l)
{for (int i=0; i<l; i++){if (p[x].next[s[i]-'a']<0){p[x].next[s[i]-'a']=top;create(top++);}x=p[x].next[s[i]-'a'];}if (!p[x].flag){p[x].flag=1;ans++;}
}
int main()
{int t,l,i,j,k,q;char s[128],s1[128],s2[128];scanf("%d",&t);while(t--){top=ans=0;create(top++);scanf("%s",s);l=(int)strlen(s);for (i=0; i<l; i++){k=q=0;for (j=0; j<i; j++)s1[k++]=s[j];for (j=i; j<l; j++)s2[q++]=s[j];s1[k++]=s2[q++]=0;strcat(s2,s1);add(0,s2,(int)strlen(s2));k=q=0;for (j=i-1; j>=0; j--)s1[k++]=s[j];for (j=i; j<l; j++)s2[q++]=s[j];s1[k++]=s2[q++]=0;strcat(s1,s2);add(0,s1,(int)strlen(s1));k=q=0;for (j=i-1; j>=0; j--)s1[k++]=s[j];for (j=i; j<l; j++)s2[q++]=s[j];s1[k++]=s2[q++]=0;strcat(s2,s1);add(0,s2,(int)strlen(s2));k=q=0;for (j=0; j<i; j++)s1[k++]=s[j];for (j=l-1; j>=i; j--)s2[q++]=s[j];s1[k++]=s2[q++]=0;strcat(s1,s2);add(0,s1,(int)strlen(s1));k=q=0;for (j=0; j<i; j++)s1[k++]=s[j];for (j=l-1; j>=i; j--)s2[q++]=s[j];s1[k++]=s2[q++]=0;strcat(s2,s1);add(0,s2,(int)strlen(s2));k=q=0;for (j=i-1; j>=0; j--)s1[k++]=s[j];for (j=l-1; j>=i; j--)s2[q++]=s[j];s1[k++]=s2[q++]=0;strcat(s1,s2);add(0,s1,(int)strlen(s1));}printf("%d\n",ans);}return 0;
}

POJ 3007 - Organize Your Train part II相关推荐

  1. poj 3007 Organize Your Train part II (哈希)

    题目:http://poj.org/problem?id=3007 题目不难,,,如果用STL的话会很简单,但会TLE 后来我敲了一个tri树..爆内存.. 又改成哈希以后结果...还是TLE... ...

  2. POJ3007 Organize Your Train part II

    Hash表来做,key值,链地址法.key公式等于字符串中所有字符asc2码乘对应位数的和 需要的函数 1.将s拆分为s1,s2 2.反转字符串 3.俩字符串合并 4.计算k值 5.建立并生成hash ...

  3. poj-3007 Organize Your Train part II

    题意很好理解,看图都能懂,就不解释了.关键就在于训练计划上的分类,神马标准模板库的应用都是骗人的,不过小用一点也是能过的,不过时间就是800,900的.总之一点也不用c++库函数就稳稳的对.本人是用的 ...

  4. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  5. hdu 1023 Train Problem II

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1212 Train Problem II Description As we all know the ...

  6. Train Problem II 卡特兰裸题(入门题)

    Train Problem II  题目大意:给你一个数n,表示有n辆火车,编号从1到n,从远方驶过来,问你有多少种出站的可能. 解题思路:模拟栈的问题而已.  卡特兰问题. 1 import jav ...

  7. Train Problem II(卡特兰数 组合数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1023 Train Problem II Time Limit: 2000/1000 MS (Java/ ...

  8. 目的地返回POJ 2336 动态规划(DP) Ferry Loading II

    在写这篇文章之前,xxx已写过了几篇关于改目的地返回主题的文章,想要了解的朋友可以去翻一下之前的文章 标题链接:http://poj.org/problem?id=2336 分析:想设我们要求的是第i ...

  9. HDU1023 Train Problem II

    传送门https://vjudge.net/problem/HDU-1023 解题思路: 大数和卡特兰数 实现代码: #include <iostream> #include <cs ...

最新文章

  1. 记录在Ubuntu14.04上安装ryu中遇到的各种坑
  2. 让UpdatePanel支持文件上传(2):服务器端组件
  3. 解决百度云大文件不能被其他下载器下载
  4. AKKA框架持久化入门样例
  5. ubuntu linux本地源,搭建Ubuntu 12.04 本地源
  6. 手机自动化测试:appium源码分析之bootstrap七
  7. 使用Identity Server 4建立Authorization Server (1)
  8. IDE:Eclipse查看Servlet源码
  9. 机器学习的练功方式(四)——KNN算法
  10. 现代软件工程 作业 个人项目
  11. ecplise安装flowable插件
  12. 一点一点看JDK源码(五)java.util.ArrayList 后篇之removeIf与Predicate
  13. WinDbg配置与下载 (转载)
  14. 城市天气数据获取接口
  15. python迭代器生成器
  16. 用c#实现拍拍抢拍精灵实现过程--核心代码--腾讯qq拍拍网秒杀器代码
  17. Altium Designer中mm/mil单位切换
  18. windows 护眼颜色修改
  19. 创业投资——创新工场
  20. 字体 跨域访问_21个访问量最大的免费字体网站

热门文章

  1. C语言中main函数参数使用
  2. JavaScript-流程控制语句(学习用)
  3. win10系统运行不了php,技术给你说win10如何打开php文件的具体解决技巧
  4. 经典算法案例001-08:如何使用质数设计扫雷(Minesweeper)游戏
  5. 【软件与系统安全】AFL模糊测试实验
  6. 第十四届蓝桥杯C++ B组——冶炼金属
  7. Flash整站项目Gaia框架总结(3)项目管理
  8. 网络安全协议分析-wireshark流量监控(未完)
  9. STM32CubeMX | 28 - STM32片内Flash的使用
  10. 金融科技再次进化 凡普金科眼中的智惠金融究竟什么样?