POJ 1635 Subway tree systems

来源:互联网 发布:车主采集软件 编辑:程序博客网 时间:2024/06/08 01:01
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
same

different

有根树的同构判断

最小表示

#include<map>#include<cmath>    #include<queue> #include<string>#include<vector>#include<cstdio>    #include<cstring>  #include<iostream>#include<algorithm>    using namespace std;#define ms(x,y) memset(x,y,sizeof(x))    #define rep(i,j,k) for(int i=j;i<=k;i++)    #define per(i,j,k) for(int i=j;i>=k;i--)    #define loop(i,j,k) for (int i=j;i!=-1;i=k[i])    #define inone(x) scanf("%d",&x)    #define intwo(x,y) scanf("%d%d",&x,&y)    #define inthr(x,y,z) scanf("%lf%lf%lf",&x,&y,&z)    typedef long long LL;const int low(int x) { return x&-x; }const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int N = 3e3 + 10;int T, n;char a[N], b[N];string s[N];bool cmp(string a, string b){return a + b < b + a;}void solve(char *g, int l, int r){for (int i = l + 1, j, k; i < r; i = j + 1){for (j = i, k = 0; j < r; j++){if (!(k += g[j] == '0' ? -1 : 1)) break;}solve(g, i, j);}int cnt = 0;for (int i = l + 1, j, k; i < r; i = j + 1){s[cnt].clear();for (j = i, k = 0; j < r; j++){s[cnt] += g[j];if (!(k += g[j] == '0' ? -1 : 1)) break;}cnt++;}sort(s, s + cnt, cmp);for (int i = 0, j = l, k; i < cnt; i++){for (k = 0; k < s[i].size(); k++) g[++j] = s[i][k];}}int main(){for (inone(T); T--;){scanf("%s%s", a + 1, b + 1);n = strlen(a + 1);solve(a, 0, n + 1);solve(b, 0, n + 1);printf("%s\n", strcmp(a + 1, b + 1) ? "different" : "same");}return 0;}

hash
#include<map>#include<ctime>#include<cmath>    #include<queue> #include<string>#include<vector>#include<cstdio>    #include<cstring>  #include<iostream>#include<algorithm>    using namespace std;#define ms(x,y) memset(x,y,sizeof(x))    #define rep(i,j,k) for(int i=j;i<=k;i++)    #define per(i,j,k) for(int i=j;i>=k;i--)    #define loop(i,j,k) for (int i=j;i!=-1;i=k[i])    #define inone(x) scanf("%d",&x)    #define intwo(x,y) scanf("%d%d",&x,&y)    #define inthr(x,y,z) scanf("%lf%lf%lf",&x,&y,&z)    typedef long long LL;const int low(int x) { return x&-x; }const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int N = 3e3 + 10;int T, n, h[N];char a[N], b[N];int get(char *s, int t, int l, int r){int hs = h[t];for (int i = l + 1, j, k; i < r; i = j + 1){for (j = i, k = 0; j < r; j++){if (!(k += s[j] == '0' ? -1 : 1)) break;}hs = (1LL * get(s, t + 1, i, j) * h[t] + hs) % mod;}return 1LL * hs * hs % mod;}int main(){srand((int)time(0));rep(i, 0, N - 1) h[i] = rand() % mod;for (inone(T); T--;){scanf("%s%s", a, b);n = strlen(a);int ha = get(a, 1, -1, n);int hb = get(b, 1, -1, n);printf("%s\n", ha != hb ? "different" : "same");}return 0;}

0 0
原创粉丝点击