POJ1635(Subway tree systems)
来源:互联网 发布:fedora yum 编辑:程序博客网 时间:2024/05/18 01:02
Subway tree systems
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 7161 Accepted: 2982
Description
Some major cities have subway systems in the form of a tree, i.e. between any pair of stations, there is one and only one way of going by subway. Moreover, most of these cities have a unique central station. Imagine you are a tourist in one of these cities and you want to explore all of the subway system. You start at the central station and pick a subway line at random and jump aboard the subway car. Every time you arrive at a station, you pick one of the subway lines you have not yet travelled on. If there is none left to explore at your current station, you take the subway line back on which you first came to the station, until you eventually have travelled along all of the lines twice,once for each direction. At that point you are back at the central station. Afterwards, all you remember of the order of your exploration is whether you went further away from the central station or back towards it at any given time, i.e. you could encode your tour as a binary string, where 0 encodes taking a subway line getting you one station further away from the central station, and 1 encodes getting you one station closer to the central station.
Input
On the first line of input is a single positive integer n, telling the number of test scenarios to follow.Each test scenario consists of two lines, each containing a string of the characters '0' and '1' of length at most 3000, both describing a correct exploration tour of a subway tree system.
Output
exploration tours of the same subway tree system, or the text "different" if the two strings cannot be exploration tours of the same subway tree system.
Sample Input
20010011101001011010001101100101101001011001001110011000111010101
Sample Output
samedifferent题意:给出两棵树的深搜遍历表示,0表示深度加1,1表示回溯,深度减1。问两棵树是否相同。
思路:
方法一:dfs
#include<cstdio>#include<vector>#include<algorithm>#include<string>#include<iostream>#include<cstring>using namespace std;char a[3005],b[3005];string dfs(char *arr, int st, int ed){ if(st>=ed){ return "01"; } int cnt=0,j=st; vector<string> re; re.clear(); for(int i = st; i <= ed; i++){ if(arr[i]=='0')cnt++; else cnt--; if(cnt==0){ re.push_back(dfs(arr,j+1,i-1)); j=i+1; } } sort(re.begin(),re.end()); string ans="0"; for(int i = 0; i < re.size();i++) ans+=re[i]; ans+="1"; return ans;}int main(){ int t; scanf("%d",&t); while(t--){ scanf("%s%s",a,b); int len1=strlen(a),len2=strlen(b); if(len1!=len2){ printf("different\n"); continue; } string ans1=dfs(a,0,len1-1); string ans2=dfs(b,0,len2-1); if(ans1==ans2)printf("same\n"); else printf("different\n"); } return 0;}
方法二:
hash大法。
#include<cstdio>#include<cstdlib>#include<iostream>#include<cstring>using namespace std;const int Mod=3001,maxn=3005;char a[maxn],b[maxn];int has[maxn];char *now;int _hash(int cur){ int re=has[cur]; while(*now!='\0'&&*now++=='0'){ re = (re+_hash(cur+1)*has[cur])%Mod; } return (re*re)%Mod;}int main(){ int t; for(int i = 0; i < maxn; i++) has[i]=rand()%Mod; scanf("%d",&t); while(t--){ scanf("%s%s",a,b); int len1=strlen(a),len2=strlen(b); if(len1!=len2){ printf("different\n"); continue; } now=a; int ans1=_hash(1); now=b; int ans2=_hash(1); if(ans1==ans2)printf("same\n"); else printf("different\n"); } return 0;}
0 0
- POJ1635(Subway tree systems)
- poj1635 - Subway tree systems
- POJ1635 Subway tree systems
- 树的最小表示法 poj1635 Subway tree systems
- [POJ1635]Subway tree systems 判断有根树的同构 有根树哈希
- 地铁系统 POJ1635 subway tree systems 判断树同构 DFS搜索子串
- POJ 1635 Subway tree systems
- poj 1635 Subway tree systems
- poj 1635 Subway tree systems
- POJ 1635 Subway tree systems
- poj 1635 Subway tree systems
- Sicily 1064. Subway Tree Systems
- POJ 1635 Subway tree systems
- POJ 1635 Subway tree systems
- POJ 1635 Subway tree systems
- POJ 1635 Subway tree systems
- Poj 1635 Subway tree systems (树的最小表示)
- poj 1635 Subway tree systems(树的同构,经典)
- cs193P assignmentV 问题1
- hdoj 2141 can you find it ?
- cocos2d-x改进的粒子系统和编辑器(需求分析)
- uva 120 Stacks of Flapjacks(练习deque)
- UVA - 1161 Objective: Berlin(最大流+时序模型)
- POJ1635(Subway tree systems)
- C++类构造函数初始化列表
- c++ 构造函数初始化列表
- 【ZOJ2334】Monkey King
- Oracle设置表中自增字段
- php动态调用函数方法&&非关系数据库的优势
- HDU 3232 && UVA 12230 (简单期望)
- HDU 5400 Arithmetic Sequence(数学,容斥)
- 华为机试题:字符串最后一个单词的长度