Sicily 1064. Subway Tree Systems

来源:互联网 发布:苏州二手房成交数据 编辑:程序博客网 时间:2024/06/08 12:46

1064. Subway Tree Systems

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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 traveled 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 traveled 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.

 

Figure 1. To the left: A subway tree system. The larger dot is the central station. To the right: Three out of several possible encodings of exploration tours for the subway system.

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

For each test scenario, output one line containing the text "same" if the two strings may encode 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

same

different

// Problem#: 1064// Submission#: 3228142// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <algorithm>#include <iostream>#include <string>#include <stdio.h>#include <queue>#include <string.h>#include <vector>#include <iomanip>#include <map>#include <stack>#include <functional>#include <list>#include <cmath>using namespace std;#define MAX 3005void findExpression(string & s, int st, int ed) {    if (st >= ed) return;    string * sub = new string[ed - st];    int counter = 0;    int num = 0;    int pos = st;    for (int i = st; i <= ed; i++) {        if (s[i] == '1') counter++;        else counter--;        if (counter == 0) {            sub[num++] = s.substr(pos, i - pos + 1);            pos = i + 1;        }    }    for (int i = 0; i < num; i++) {        findExpression(sub[i], 1, sub[i].size() - 2);    }    sort(sub, sub + num);    for (int i = 0, k = st; i < num; i++) {        int length = sub[i].size();        for (int j = 0; j < length; j++) {            s[k++] = sub[i][j];        }    }}int main() {    std::ios::sync_with_stdio(false);    int caseNum;    cin >> caseNum;    while (caseNum--) {        string a, b;        cin >> a >> b;        findExpression(a, 0, a.size() - 1);        findExpression(b, 0, b.size() - 1);        if (a == b) cout << "same" << endl;        else cout << "different" << endl;    }    //getchar();    //getchar();        return 0;}                                 


0 0
原创粉丝点击