/*主要是根据输入串建树,然后对树DFS,重新获得一个排序后的01串。
对两个输入串分别运用上述方法分别得到两个排序后的01串,如果两个串相等则same,否则different
关键在于DFS时对子树返回的01串进行存储和排序,然后重组
比如对于
0100101100100111和0011000111010101,重组后分别变成:
0001101100101101和0001110011010101,不相等所以输出different
*/
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#define MAX_N 3000
using namespace std;
struct tree
{
tree *parent;
tree *next; //第一个儿子
tree *bro; //兄弟
tree()
{
parent = next = bro = NULL;
}
};
//建树
tree *buildTree(string str)
{
int p;
char ch;
tree *root = new tree();
tree *cur = root;
for(p = 0; p < str.length(); p++)
{
ch = str[p];
if(ch == '0')
{
tree *temp = new tree();
temp->parent = cur;
tree *nptr = cur;
if(nptr->next)
{
nptr = nptr->next;
while(nptr->bro)
nptr = nptr->bro;
nptr->bro = temp;
}
else
nptr->next = temp;
cur = temp;
}
else if(ch == '1')
{
cur = cur->parent;
}
}
return root;
}
tree *beginRoot;
//重组DFS,获得排序后的01串
string travelSeq(tree *root)
{
tree *nptr = root;
vector<string> vec;
//遍历所有儿子
if(nptr->next)
{
vec.push_back(travelSeq(nptr->next));
nptr = nptr->next;
while(nptr->bro)
{
vec.push_back(travelSeq(nptr->bro));
nptr = nptr->bro;
}
}
//排序
sort(vec.begin(), vec.end());
vector<string>::iterator iter = vec.begin();
string res = "";
//组合
for(; iter != vec.end(); iter++)
res += *iter;
if(root != beginRoot) res = '0' + res + '1';
return res;
}
int main()
{
int caseN;
string str1, str2;
scanf("%d", &caseN);
while(caseN--)
{
cin>>str1>>str2;
beginRoot = buildTree(str1);
str1 = travelSeq(beginRoot);
beginRoot = buildTree(str2);
str2 = travelSeq(beginRoot);
if(str1 == str2)
printf("same/n");
else
printf("different/n");
}
return 0;
}
相关推荐
自抗扰控制ADRC原理解析及案例应用
ESO的设计通常基于系统动态方程的线性化或近似模型,其输出包括系统状态的估计值和总扰动的估计值。非线性状态误差反馈控制律(Nonlinear State Error Feedback,NLSEF)是ADRC的最后一个环节,负责根据ESO和TD提供的信息生成控制信号。ADRC的另一个重要发展方向是与其他控制策略的融合,如模糊控制、神经网络和自适应控制等,以进一步提高系统的鲁棒性和适应性。ADRC的参数主要包括扩张状态观测器(ESO)的参数和非线性状态误差反馈控制律(NLSEF)的参数。
图的重构:Subway Tree System
Subway tree systems Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5621 Accepted: 2316 Description Some major cities have subway systems in the form of a tr
AI计算,为什么要用GPU?
几年后,2012年,神经网络之父杰弗里·辛顿(Geoffrey Hinton)的两个学生——亚历克斯·克里切夫斯基(Alex Krizhevsky)、伊利亚·苏茨克沃(Ilya Sutskever),利用“深度学习+GPU”的方案,提出了深度神经网络AlexNet,将识别成功率从74%提升到85%,一举赢得Image Net挑战赛的冠军。根据形态,GPU可分为独立GPU(dGPU,discrete/dedicated GPU)和集成GPU(iGPU,integrated GPU),也就是常说的独显、集显。
POJ-1635:Subway tree systems(树的最小表示法)
POJ-1635:Subway tree systems(树的最小表示法) 判断两棵树是否同构
四阶龙格库塔法求解一次常微分方程组(python实现)
之前在博客发布了关于使用四阶龙格库塔方法求解一次常微分方程组的文章,由于代码缺少具体的验证,部分朋友可能存在疑问,因此这里打算再重新写一篇博客来验证一下程序的正确性,另外,这里是使用python语言来实现的。
从BUCK-BOOST到反激:一个‘拆电感’的视角,轻松理解多路输出交叉调整
本文从BUCK-BOOST电路到反激变换器的演变入手,通过‘拆解电感’的独特视角解析多路输出交叉调整机制。深入探讨了反激电路的能量暂存方式、传递路径变化及电压关系,并结合具体参数对比揭示其特性。针对多路输出中的电压平衡问题,提供了绕组排布优化、元器件选型等实战技巧,帮助工程师有效提升电源系统的稳定性与效率。
树的最小表示法 poj1635 Subway tree systems
传送门:点击打开链接 题意:有根树,0表示远离根节点,1表示接近根节点,一条边只访问一次。现在告诉你两种访问方式,问这两棵树是否同构异形。 思路:从下向上把子树排序,这样向上,最后表示出来的就是最小表示的了,那么就有序了,就可以直接比较字符串就能判断两棵树是否相同。 #include #include #include #include #include #include #include
多思计组实验实验三、存储器读写实验
实验三、存储器读写实验
poj 1635 Subway tree systems
Subway tree systems Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6203 Accepted: 2580 Description Some major cities have subway systems in the form of a tr
手把手教你使用YOLOv11完成人脸分类识别,YOLOv11人脸识别,解决中文乱码问题,全网最详细教程
手把手教你使用YOLOv11完成人脸分类识别,YOLOv11人脸识别,解决中文乱码问题,全网最详细教程
【k8s故障处理篇】calico-kube-controllers状态为“ImagePullBackOff”解决办法
【k8s故障处理篇】calico-kube-controllers状态为“ImagePullBackOff”解决办法
POJ1635(Subway tree systems)
Subway tree systems Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7161 Accepted: 2982 Description Some major cities have subway systems in the form of
Data-Structures-and-Algorithm-Analysis-in-Java.pdf
作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...
poj1635 Subway tree systems
把0看成1 1看成-1 对于一个子树来讲如果从开头到某一段的和为0表示又回到了这个子树的根 根据子树把字符串截乘小段 再sort string 方便比较两个树是否相同 注意截成的小段递归求解时要把开头和结尾的0和1去掉 才算是以这个子树的根为起点(开头本来表示进入这个根,结尾本来表示离开这个根) 好用的stl: ss=s.substr(a,b) a为起点下标,b为长度 sort(vs,begin(...
Statistics and Machine Learning Toolbox
Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具
RU DOS版、EFI版32bit/64bit BIOS常用工具
DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。
POJ1635 Subway tree systems
POJ1635 Subway tree systems Time Limit:1000MS Memory Limit:10000K Total Submissions:5470 Accepted:2248 Description Some major cities have subway systems in the form...
Camera2Demo
在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作
POJ - 1635 Subway tree systems(树的最小表示)
POJ - 1635 Subway tree systems #include<iostream> #include<algorithm> #include<vector> using namespace std; string dfs(string &a, int &u) { u++; vector<string> b; while(a[u]=='0') b.push_back(dfs(a,u)); u+
[ POJ ][ 树HASH ] 1635 Subway tree systems
首先,对于一个树,我们可以有一种压缩表示: 0010011101001011 其中 0 表示向下走,1 表示向上走。于是: 00100111|01|001011 对于每一段的 0 1 出现次数相同,这种hash方法叫树的最小表示法。 1635 题目精简大意:给你n对01字符串,判断每一对儿表示的是不是同一个树,方法: 1.定义 cnt, start, end ...
POJ1635 Subway tree systems
用CodeBlock写题,一个VS党深受折磨,文本编辑器一般般,编译错误的提示死活看不懂 好吧,毕竟是开源的 此题题意: 给出两串含有‘1’和‘0’的字符串,0表示向下搜索,1表示回溯,这样深搜一颗树 深搜完之后问这两棵树是不是同一棵树,因为树的结点顺序不同,所以可以导致树深搜的序列也不同 用最小表示法表示一颗子树 比如样例中的 0010011101001011 有
poj1635 - Subway tree systems
想看更多的解题报告: http://blog.youkuaiyun.com/wangjian8006/article/details/7870410 转载请注明出处:http://blog.youkuaiyun.com/wangjian8006 题目大意:给出两串含有‘1’和‘0’的字符串,0表示向下搜索,1表示回溯,这样深搜一颗树 深搜完之后
题解 POJ1635 【Subway tree systems】
题目链接:Link Problem Solution 这道题是一道比较裸的树哈希的题目,注意为了避免被卡,不同深度的节点的权值不同且是随机的。 Hash千万别用自然溢出!!! Code #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> usi...
[有向树的最小表示] poj 1635 Subway tree systems
题目链接: http://poj.org/problem?id=1635 题目意思: 判断两棵树是否为同构的。0
9285



被折叠的 条评论
为什么被折叠?



