zoukankan      html  css  js  c++  java
  • POJ1635Subway tree systems

    Subway tree systems
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 8049   Accepted: 3357

    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

    Source

     
    【题解】
    树的括号序列最小表示法,0相当于'(',1相当于')'https://www.byvoid.com/zhs/blog/directed-tree-bracket-sequence
    直接在序列上递归到叶子,然后从叶子向上合并,交换子树位置,小的子树在前面
    我破例用了死活不想用的又慢又不自由的string。
    STL用起来真是爽啊
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <string>
     6 #include <vector>
     7 #include <algorithm>
     8 std::string s1, s2;
     9 int t;
    10 std::string dfs(std::string now)
    11 {
    12     std::vector<std::string> s;
    13     std::string re = "";
    14     int flag = 0, pre = 1;
    15     for(register int i = 0;i < now.size();++ i)
    16     {
    17         if(now[i] == '0')++ flag;
    18         else -- flag;
    19         if(flag == 0)
    20         {
    21             std::string tmp = dfs(now.substr(pre, i - pre));
    22             if(tmp == "")s.push_back("01");
    23             else s.push_back('0' + tmp + '1');
    24             pre = i + 2;
    25         }
    26     }
    27     std::sort(s.begin(), s.end());
    28     for(register int i = 0;i < s.size();++ i)re += s[i];
    29     return re;
    30 }
    31 int main()
    32 {
    33     scanf("%d", &t);
    34     for(;t;-- t)
    35     {
    36         std::cin >> s1 >> s2;
    37         if(dfs(s1) == dfs(s2))printf("same
    ");
    38         else printf("different
    ");
    39     }
    40     return 0;
    41 } 
    POJ1635
     
  • 相关阅读:
    luogu_1339 [USACO09OCT]热浪Heat Wave
    luogu_1341 无序字母对
    luogu_1330 封锁阳光大学
    luogu_3383 【模板】线性筛素数
    luogu_1095 守望者的逃离
    luogu_1373 小a和uim之大逃离
    查看寄存器
    Assembly oth
    非常详细的端口表汇总
    公式证明
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/7477976.html
Copyright © 2011-2022 走看看