Organize Your Train part II-POJ3007模拟
Organize Your Train part II
Time Limit: 1000MS | Memory Limit: 65536K |
---|
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
思路:简单的模拟,不过不能用map来标记字符串,会超时,所以写一个哈希邻接表
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
const int Max = 99991;
typedef struct node
{
char str[80];
node *next;
node()
{
next = 0;
}
}Point;
Point *Head[Max];
int ans;
bool Hash(char *s) //哈希处理冲突
{
int len = strlen(s);
int num = 0 ;
for(int i=0;i<len;i++)
{
num = num+s[i]*(i+1);
num%=Max;
}
if(Head[num]==NULL)
{
Point *p = new Point;
strcpy(p->str,s);
Head[num]=p;
return true;
}
else
{
Point *p = Head[num];
while(p)
{
if(strcmp(p->str,s)==0)
{
return false;
}
p=p->next;
}
p=new Point;
strcpy(p->str,s);
p->next = Head[num];
Head[num] = p;
}
return true;
}
void Reverse(char *s,int len)//字符串翻转
{
for(int i = 0 ;i<len/2;i++)
{
swap(s[i],s[len-1-i]);
}
}
void Strcat(char *s1,char *s2,char *s)
{
int i=0,j;
for(j=0;s1[j]!='\0';j++)
{
s[i++]=s1[j];
}
for(j=0;s2[j]!='\0';j++)
{
s[i++]=s2[j];
}
s[i]='\0';
}
void Strstr(char *s,char *s1,char *s2,int mid,int len)//字符串合并
{
for(int i=0;i<mid;i++)
{
s1[i]=s[i];
}
s1[mid]='\0';
for(int i=mid,j=0;i<len;j++,i++)
{
s2[j]=s[i];
}
s2[len-mid]='\0';
}
void Add(char *s1,char *s2,char *s)//判断字符
{
Strcat(s1,s2,s);
if(Hash(s))
{
ans++;
}
Strcat(s2,s1,s);
if(Hash(s))
{
ans++;
}
}
int main()
{
int T;
scanf("%d",&T);
char s1[80],s2[80],s[80],str[80];
while(T--)
{
scanf("%s",str);
memset(Head,0,sizeof(Head));
ans=0;
int len=strlen(str);
for(int i=1;i<=len-1;i++)
{
Strstr(str,s1,s2,i,len);
Add(s1,s2,s);
Reverse(s1,i);
Add(s1,s2,s);
Reverse(s2,len-i);
Add(s1,s2,s);
Reverse(s1,i);
Add(s1,s2,s);
}
printf("%d\n",ans);
}
return 0;
}
Organize Your Train part II-POJ3007模拟的更多相关文章
- POJ 3007 Organize Your Train part II
题意: 如上图所示,将一个字符串进行分割,反转等操作后不同字符串的个数: 例如字符串abba:可以按三种比例分割:1:3:2:2:3:1 部分反转可以得到如下所有的字符串: 去掉重复可以得到六个不同的 ...
- POJ 3007 Organize Your Train part II (字典树 静态)
Organize Your Train part II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6478 Acce ...
- Organize Your Train part II 字典树(此题专卡STL)
Organize Your Train part II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8787 Acce ...
- POJ 3007:Organize Your Train part II
Organize Your Train part II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7561 Acce ...
- poj 3007 Organize Your Train part II(静态字典树哈希)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6700 Accepted: 1922 Description RJ Freigh ...
- 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 Organize Your Train part II
http://poj.org/problem?id=3007 #include<cstdio> #include<algorithm> #include<cstring& ...
- Organize Your Train part II(hash)
http://poj.org/problem?id=3007 第一次用STL做的,TLE了,自己构造字符串哈希函数才可以.. TLE代码: #include <cstdio> #inclu ...
随机推荐
- window的对象有哪些(笔记)
window的主对象主要有如下几个: document 对象: frames 对象: history 对象: location 对象: navigator 对象: screen 对象: 全局变量和函数 ...
- 7. ensemble learning & AdaBoost
1. ensemble learning 集成学习 集成学习是通过构建并结合多个学习器来完成学习任务,如下图: 集成学习通过将多个学习学习器进行结合,常可以获得比单一学习器更优秀的泛化性能 从理论上来 ...
- Java实现归并排序
package Sort; import java.util.Arrays; public class MergeSort { public static void merge(int[] list, ...
- mybatis比较数字或者单字母
http://blog.csdn.net/alibert/article/details/50177017 <if test="req.queryType == '3'.toStrin ...
- 偶遇到 java.util.ConcurrentModificationException 的异常
今天在调试程序 遇到了如此问题 贴上代码来看看稍后分析 List<String> list = null;boolean isUpdate = false;try { list = JSO ...
- js ajax同步请求造成浏览器假死的问题
一.问题的起因 今天做一个需求遇到了这么个情况,就是用户个人中心有个功能,点击按钮,可以刷新用户当前的积分,这个肯定需要使用到ajax的同步请求了,当时喀喀喀三下五除二写玩了,大概代码如下: /** ...
- GPS部标监控平台的架构设计(十一)-基于Memcached的分布式Gps监控平台
部标gps监控平台的架构,随着平台接入的车辆越来越多,架构也面临越来越大的负载挑战,我们当然希望软件尽可能的优化并能够接入更多的车辆,减少在硬件上的投资.但是当车辆增多到某一个临界点的时候,仍然要面临 ...
- python requests的安装与简单运用
requests是python的一个HTTP客户端库,跟urllib,urllib2类似,那为什么要用requests而不用urllib2呢?官方文档中是这样说明的: python的标准库urllib ...
- 基于ssh框架的在线考试系统开发的质量属性
我做的系统是基于ssh框架的在线考试系统.在线考试系统有以下几点特性:(1)系统响应时间需要非常快,可以迅速的出题,答题.(2)系统的负载量也需要非常大,可以支持多人在线考试(3)还有系统的安全性也需 ...
- 【原创】如何在Android Studio下调试原生安卓Framework层面的源代码
1. Open Existing Android Studio Project. 2. 打开后, Projects -> Android 里面是空的. 这时候,需要选到 Projects-> ...