Organize Your Train part II【POJ--3007】【平衡二叉树】
2015-12-19 16:09
513 查看
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):
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.
题意:给你一串字符串,将该字符串分成两部分然后再拼接起来,拼接的时候,所分开的两部分可以正着拼接
也可以有一个倒着拼接或两个都倒着拼接(详细可见样例),问一串字符串可以拼接出几串不同的字符串.
思路:由于所给字符串的长度并不长,可以枚举每一种可能,只要没出现过的就计数器加一,那么问题来了,
怎么判断已拼接好的字符串是否出现过?我的做法是用平衡二叉树来记录。拿着已拼接好的字符串去建好的平
衡二叉树中找,看树上是否有,如果没有就说明没出现过,计数器加一并且将该字符串加到该二叉树上。该做
法就相当于平衡二叉树中找关键值,时间复杂度也就log(n)。
吐槽:其实这个题刚开始我并没有思路,想了一两天还是没想出来,就搜了下题解,刚打开题解看到他说用哈
希表哈希,刚看到这我就有了思路,因为刚好刚学完平衡二叉树,我想着我不如用平衡二叉树做一下说不准可
能比哈希更好操作呢,然后就开始用平衡二叉树来做这道题。还有就是,这个题麻烦的就是枚举字符串的时候那个字符串拼接有点不好操作(一会你看了代码就知道了23333)。敲完之后发现代码好麻烦啊
过了样例
就交了一发,wccccc居然过了!!!然后我就赶紧看前两天搜到的那个题解,看人家哈希是不是比我的简单
点,结果是:然而并不简单!而并不简单!并不简单!不简单!简单!单!!
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
Sample Output
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.
题意:给你一串字符串,将该字符串分成两部分然后再拼接起来,拼接的时候,所分开的两部分可以正着拼接
也可以有一个倒着拼接或两个都倒着拼接(详细可见样例),问一串字符串可以拼接出几串不同的字符串.
思路:由于所给字符串的长度并不长,可以枚举每一种可能,只要没出现过的就计数器加一,那么问题来了,
怎么判断已拼接好的字符串是否出现过?我的做法是用平衡二叉树来记录。拿着已拼接好的字符串去建好的平
衡二叉树中找,看树上是否有,如果没有就说明没出现过,计数器加一并且将该字符串加到该二叉树上。该做
法就相当于平衡二叉树中找关键值,时间复杂度也就log(n)。
吐槽:其实这个题刚开始我并没有思路,想了一两天还是没想出来,就搜了下题解,刚打开题解看到他说用哈
希表哈希,刚看到这我就有了思路,因为刚好刚学完平衡二叉树,我想着我不如用平衡二叉树做一下说不准可
能比哈希更好操作呢,然后就开始用平衡二叉树来做这道题。还有就是,这个题麻烦的就是枚举字符串的时候那个字符串拼接有点不好操作(一会你看了代码就知道了23333)。敲完之后发现代码好麻烦啊
过了样例
就交了一发,wccccc居然过了!!!然后我就赶紧看前两天搜到的那个题解,看人家哈希是不是比我的简单
点,结果是:然而并不简单!而并不简单!并不简单!不简单!简单!单!!
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
<span style="font-size:18px;">#include <cstdio> #include <cstring> #include <string> #include <algorithm> using namespace std; int cnt,len; struct node //平衡二叉树的结点 { char name[100]; node *l,*r; int nheight; }; int height(node *p) //当前结点的高度值 { if(p==NULL) return -1; return p->nheight; } node *LLRoate(node *p) //对LL型直接在不平衡结点处进行左旋转 { node *p1; p1=p->l; p->l=p1->r; p1->r=p; p->nheight=max(height(p->l),height(p->r))+1; //结点的位置变了,要更新结点的高度值 p1->nheight=max(height(p1->l),p->nheight)+1; return p1; } node *RRRotate(node *p) //对RR型直接在不平衡结点处进行右旋转 { node *p1; p1=p->r; p->r=p1->l; p1->l=p; p->nheight=max(height(p->l),height(p->r))+1; //结点的位置变了,要更新结点的高度值 p1->nheight=max(height(p1->r),p->nheight)+1; return p1; } node *LRRoate(node *p) { p->l=RRRotate(p->l); //在不平衡结点的左儿子处进行右旋转 return LLRoate(p); //在不平衡结点处进行左旋转并返回新的根 } node *RLRotate(node *p) { p->r=LLRoate(p->r); //在不平衡结点的右儿子处进行左旋转 return RRRotate(p); //在不平衡结点处进行右旋转并返回新的根 } void _Insert(char *s,node *&p) //查找该字符串是否出现过 { if(p==NULL) //若没出现过,计数器加1并且将该字符串假如到该树上 { p=new node; strcpy(p->name,s); p->nheight=0; p->l=p->r=NULL; cnt++; } else if(strcmp(s,p->name)<0) { _Insert(s,p->l); if(height(p->l)-height(p->r)==2) { if(strcmp(s,p->l->name)<0) p=LLRoate(p); else p=LRRoate(p); } } else if(strcmp(s,p->name)>0) { _Insert(s,p->r); if(height(p->l)-height(p->r)==-2) { if(strcmp(s,p->r->name)>0) p=RRRotate(p); else p=RLRotate(p); } } p->nheight=max(height(p->l),height(p->r))+1; } int main() { int n; char st[100]; scanf("%d",&n); while(n--) { scanf("%s",st); len=strlen(st); if(len==1) { printf("1\n"); continue; } cnt=0; node *head=NULL; for(int i=1; i<len; i++) //枚举所分开的第一个字符串的长度 { char s1[100],s2[100],s3[100],s4[100]; memset(s1,0,sizeof(s1)); memset(s2,0,sizeof(s2)); memset(s3,0,sizeof(s3)); memset(s4,0,sizeof(s4)); int k,ans=0; for(k=0; k<i; k++) s1[ans++]=st[k]; //前一部分字符串 ans=0; for(; k<len; k++) s2[ans++]=st[k]; //后一部分字符串 ans=0; for(int j=i-1; j>=0; j--) s3[ans++]=s1[j]; //前一部分字符串的逆字符串 ans=0; for(int j=len-i-1; j>=0; j--) s4[ans++]=s2[j]; //后一部分字符串的逆字符串 char ss1[100]; //枚举所拼接的8种情况 strcpy(ss1,s1); strcat(ss1,s2); _Insert(ss1,head); char ss2[100]; strcpy(ss2,s1); strcat(ss2,s4); _Insert(ss2,head); char ss3[100]; strcpy(ss3,s3); strcat(ss3,s2); _Insert(ss3,head); char ss4[100]; strcpy(ss4,s3); strcat(ss4,s4); _Insert(ss4,head); char ss5[100]; strcpy(ss5,s2); strcat(ss5,s1); _Insert(ss5,head); char ss6[100]; strcpy(ss6,s4); strcat(ss6,s1); _Insert(ss6,head); char ss7[100]; strcpy(ss7,s2); strcat(ss7,s3); _Insert(ss7,head); char ss8[100]; strcpy(ss8,s4); strcat(ss8,s3); _Insert(ss8,head); } printf("%d\n",cnt); } return 0; } </span>
相关文章推荐
- RabbitMQ消息队列1: Detailed Introduction 详细介绍
- POJ1671--Painer'sProblem
- Foxmail重复收取邮件解决方案
- foxmail重复收取邮件解决办法及删除重复邮件办法
- 线程同步(同步对象、同步访问对象)http://blog.csdn.net/akof1314/article/details/5764200
- 性能测试中AIX服务器资源监控与瓶颈分析-1
- 能测试中AIX服务器资源监控与瓶颈分析-2
- RAID级别和实现实例操作详解
- 高斯定时器导致的NotHttpResponseException:The target server failed to repond异常 -- 解决
- RAID各层级特性
- 创建一个10G可用空间的RAID5
- 全局对象和头文件的处理http://zhidao.baidu.com/link?url=w4sdxKWPK_Wxto1ypQGxMPjDVd_fmQCCBr_Jvkmjns8px5GuBZmBCq9n
- RAID
- [BetterExplained]书写是为了更好的思考
- [BetterExplained]遇到问题为什么应该自己动手
- AIX 分页替换技术入门
- #ifndef/#define/#endif使用详解http://blog.csdn.net/abc5382334/article/details/18052757
- BaiduMapsApiASDemo :编译错误、baidumapsdk.demo key验证出错
- 人工智能初体验(一):使用图灵机器人智能获取问题回答
- UVa 253 Cube painting (骰子涂色)