Subway tree systems - POJ 1635 树的最小表示法

本文通过解析两个特定的字符串表示,探讨了如何识别它们是否代表同一城市地铁系统的不同探索路径。采用最小表示法简化子树描述,并通过递归解决方法实现字符串的结构化分析,最终判断两组路径是否一致。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Subway tree systems
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7145 Accepted: 2974

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

2
0010011101001011
0100011011001011
0100101100100111
0011000111010101

Sample Output

same
different

题意:用一个字符串来表示一棵有根的树,0表示向下,1表示向上,问这两个字符串表示的树是否相同。

思路:用最小表示法来表示每个节点的子树的情况。即将这个节点的子节点的最小表示法排序后相加。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
int T,t,n,m;
string s1,s2;
string solve(string str)
{
    int i,j,k,a,b,pos=0,len;
    vector<string> vc;
    len=str.length();
    string str2="";
    if(len==0)
      return str;
    a=b=0;
    for(i=0;i<len;i++)
    {
        if(str[i]=='0')
          a++;
        else
          b++;
        if(a==b && a>0)
        {
            if(a==1)
              str2="01";
            else
            {
                str2=str.substr(pos+1,i-pos-1);
                str2="0"+solve(str2)+"1";
            }
            vc.push_back(str2);
            pos=i+1;
            a=b=0;
        }
    }
    str2="";
    sort(vc.begin(),vc.end());
    for(i=0;i<vc.size();i++)
       str2+=vc[i];
    vc.clear();
    return str2;
}
int main()
{
    int i,j,k;
    scanf("%d",&T);
    for(t=1;t<=T;t++)
    {
        cin>>s1;
        cin>>s2;
        if(solve(s1)==solve(s2))
          printf("same\n");
        else
          printf("different\n");
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值