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

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

一串字符串,从中间的某一处切开,前后都可能reverse,然后这两部分前后都有可能,即正常情况下可能出现8中不同的字符串。然后问所有这些可能的字符串中有多少种不同的。

自己写hash会TLE,只能自己写。各种麻烦,函数返回局部变量的指针容易出问题,这样不行。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; #define maxn 80 int num;
//char *wang_reverse,*sub1,*sub2,*sub3,*sub4;
//char *res1,*res2,*res3,*res4,*res5,*res6,*res7,*res8;
char sub1[80],sub2[80],sub3[80],sub4[80];
char res1[80],res2[80],res3[80],res4[80],res5[80],res6[80],res7[80],res8[80];
struct no
{
char str[maxn]; }node[maxn*maxn]; void reverse2(char *str)
{
int i,j,len;
len=strlen(str);
for(i=len-1,j=0;i>=0;i--,j++)
sub3[j]=str[i];
sub3[j]='\0';
} void reverse3(char *str)
{
int i,j,len;
len=strlen(str);
for(i=len-1,j=0;i>=0;i--,j++)
sub4[j]=str[i];
sub4[j]='\0';
} void substr(char *str,int i)
{
int j; for(j=0;j<i;j++)
sub1[j]=str[j];
sub1[j]='\0';
} void substr_rever(char *str,int i)
{
int x,j,len; len=strlen(str);
for(j=len-1,x=0;j>=i;j--,x++)
sub2[x]=str[j];
sub2[x]='\0';
} void add_str1(char *re1,char *re2)
{
int len1=strlen(re1);
int len2=strlen(re2);
int i,j; for(i=0;i<len1;i++)
{
res1[i]=re1[i];
}
for(j=0;j<len2;j++)
{
res1[i]=re2[j];
i++;
}
} void add_str2(char *re1,char *re2)
{
int len1=strlen(re1);
int len2=strlen(re2);
int i,j; for(i=0;i<len1;i++)
{
res2[i]=re1[i];
}
for(j=0;j<len2;j++)
{
res2[i]=re2[j];
i++;
}
} void add_str3(char *re1,char *re2)
{
int len1=strlen(re1);
int len2=strlen(re2);
int i,j; for(i=0;i<len1;i++)
{
res3[i]=re1[i];
}
for(j=0;j<len2;j++)
{
res3[i]=re2[j];
i++;
}
} void add_str4(char *re1,char *re2)
{
int len1=strlen(re1);
int len2=strlen(re2);
int i,j; for(i=0;i<len1;i++)
{
res4[i]=re1[i];
}
for(j=0;j<len2;j++)
{
res4[i]=re2[j];
i++;
}
} void add_str5(char *re1,char *re2)
{
int len1=strlen(re1);
int len2=strlen(re2);
int i,j; for(i=0;i<len1;i++)
{
res5[i]=re1[i];
}
for(j=0;j<len2;j++)
{
res5[i]=re2[j];
i++;
}
}
void add_str6(char *re1,char *re2)
{
int len1=strlen(re1);
int len2=strlen(re2);
int i,j; for(i=0;i<len1;i++)
{
res6[i]=re1[i];
}
for(j=0;j<len2;j++)
{
res6[i]=re2[j];
i++;
}
}
void add_str7(char *re1,char *re2)
{
int len1=strlen(re1);
int len2=strlen(re2);
int i,j; for(i=0;i<len1;i++)
{
res7[i]=re1[i];
}
for(j=0;j<len2;j++)
{
res7[i]=re2[j];
i++;
}
}
void add_str8(char *re1,char *re2)
{
int len1=strlen(re1);
int len2=strlen(re2);
int i,j; for(i=0;i<len1;i++)
{
res8[i]=re1[i];
}
for(j=0;j<len2;j++)
{
res8[i]=re2[j];
i++;
}
} bool judge(char *str)
{
int i;
for(i=0;i<num;i++)
{
if(strcmp(str,node[i].str)==0)
{
return false;
}
}
return true;
} int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); char wang[80];
int test,i,len;
scanf("%d",&test); while(test--)
{
for(i=0;i<80;i++)
{
wang[i]='\0';
sub1[i]='\0';
sub2[i]='\0';
sub3[i]='\0';
sub4[i]='\0'; res1[i]='\0';
res2[i]='\0';
res3[i]='\0';
res4[i]='\0';
res5[i]='\0';
res6[i]='\0';
res7[i]='\0';
res8[i]='\0';
}
scanf("%s",wang);
num=0;
len=strlen(wang); for(i=1;i <= len-1;i++)
{
substr(wang,i);//sub1
substr_rever(wang,i);//sub2 reverse2(sub1);//sub3
reverse3(sub2);//sub4 add_str1(sub1,sub2);
add_str2(sub2,sub1); add_str3(sub3,sub2);
add_str4(sub2,sub3); add_str5(sub1,sub4);
add_str6(sub4,sub1); add_str7(sub3,sub4);
add_str8(sub4,sub3); if(judge(res1))
{
strcpy(node[num].str,res1);
num++;
}
if(judge(res2))
{
strcpy(node[num].str,res2);
num++;
}
if(judge(res3))
{
strcpy(node[num].str,res3);
num++;
}
if(judge(res4))
{
strcpy(node[num].str,res4);
num++;
}
if(judge(res5))
{
strcpy(node[num].str,res5);
num++;
}
if(judge(res6))
{
strcpy(node[num].str,res6);
num++;
}
if(judge(res7))
{
strcpy(node[num].str,res7);
num++;
}
if(judge(res8))
{
strcpy(node[num].str,res8);
num++;
}
}
printf("%d\n",num);
}
//system("pause");
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 3007:Organize Your Train part II的更多相关文章

  1. POJ 3007 Organize Your Train part II

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

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

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

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

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

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

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

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

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

  6. poj 3007 Organize Your Train part II(静态字典树哈希)

    Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6700 Accepted: 1922 Description RJ Freigh ...

  7. poj Organize Your Train part II

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

  8. Organize Your Train part II(hash)

    http://poj.org/problem?id=3007 第一次用STL做的,TLE了,自己构造字符串哈希函数才可以.. TLE代码: #include <cstdio> #inclu ...

  9. Organize Your Train part II-POJ3007模拟

    Organize Your Train part II Time Limit: 1000MS Memory Limit: 65536K Description RJ Freight, a Japane ...

随机推荐

  1. Linux centosVMware运行告警系统、分发系统-expect讲解、自动远程登录后,执行命令并退出、expect脚本传递参数、expect脚本同步文件、指定host和要同步的文件、shell项目-分发系统-构建文件分发系统、分发系统-命令批量执行

    一运行告警系统 创建一个任务计划crontab -e 每一分钟都执行一次 调试时把主脚本里边log先注释掉 再次执行 没有发现502文件说明执行成功了,每日有错误,本机IP 负载不高 二.分发系统-e ...

  2. 五、Centos7安装mysql:第一步查看系统有无mysql,然后删除之

    CentOS下MySQL的彻底卸载 原创 2015年10月12日 00:16:02 标签: 21149 编辑 删除 #################CentOS7下MySQL的卸载######### ...

  3. keep-alive的使用

    <keep-alive>是Vue的内置组件,能在组件切换过程中将状态保留在内存中,防止重复渲染DOM. <router-view>中间为组件</router-view&g ...

  4. KALI 2017 X64安装到U盘

    KALI 2017 X64安装到U盘启动(作者:黑冰) 此方法为虚拟机方法,自认为成功率很高,已经成功安装过16,32G U盘​,但也不排除有些人用拷碟方法安装这里我仅介绍虚拟机安装方法. ​1.准备 ...

  5. 常用WinAPI函数整理------------转载

    常用WinAPI函数整理原创 玩撕你 发布于2019-09-04 20:06:55 阅读数 101 收藏展开 之前的博客写了很多关于Windows编程的内容,在Windows环境下的黑客必须熟练掌握底 ...

  6. 微信小程序中,如何点击链接跳转到外部网页

    跳转到内部链接 这个我们应该都知道,通过wx.navigateTo,wx.redirectTo,wx.swtichTab等小程序内部的方法,可以直接跳转到小程序内部已经注册的(就是在app.json中 ...

  7. Oracle 提取数据表信息

    参考: https://www.progress.com/blogs/jdbc-tutorial-extracting-database-metadata-via-jdbc-driver http:/ ...

  8. 学习进度-11 RDD 编程初级实践

    一. 请到本教程官网的“下载专区”的“数据集”中下载 chapter5-data1.txt,该数据集包含 了某大学计算机系的成绩,数据格式如下所示: Tom,DataBase,80 Tom,Algor ...

  9. 车林通购车之家--购车计算器模块--算法js

    //CarCalculator.js var checkedClass = "jsq-item-check jsq-item-checked"; var uncheckedClas ...

  10. SciPy 介绍

    章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...