树的最小表示法


  给定两个有根树的dfs序,问这两棵树是否同构

  题解:http://blog.sina.com.cn/s/blog_a4c6b95201017tlz.html

题目要求判断两棵树是否是同构的,思路是用树的最小表示法去做。这里用的最小表示法就是将树的所有子树分别用1个字符串表示,要按字典序排序将他们依依连接起来。连接后如果两个字符串是一模一样的,那么他们必然是同构的。这样原问题就变成了子问题,子树又是一颗新的树。

  1. Source Code
  2. Problem: User: sdfzyhy
  3. Memory: 1160K Time: 672MS
  4. Language: G++ Result: Accepted
  5.  
  6. Source Code
  7.  
  8. //PKUSC 2013 R1 C
  9. #include<string>
  10. #include<vector>
  11. #include<cstdio>
  12. #include<cstring>
  13. #include<cstdlib>
  14. #include<iostream>
  15. #include<algorithm>
  16. #define rep(i,n) for(int i=0;i<n;++i)
  17. #define F(i,j,n) for(int i=j;i<=n;++i)
  18. #define D(i,j,n) for(int i=j;i>=n;--i)
  19. #define pb push_back
  20. using namespace std;
  21. typedef long long LL;
  22. inline int getint(){
  23. int r=,v=; char ch=getchar();
  24. for(;!isdigit(ch);ch=getchar()) if (ch=='-') r=-;
  25. for(; isdigit(ch);ch=getchar()) v=v*-''+ch;
  26. return r*v;
  27. }
  28. const int N=;
  29. /*******************template********************/
  30. string s1,s2;
  31.  
  32. string dfs(string s){
  33. vector<string>a;
  34. string ans="";
  35. int t=,st=;
  36. rep(i,s.length()){
  37. if (s[i]=='') t++;
  38. else t--;
  39. if (t==){
  40. if (i- > st+){
  41. a.pb(""+dfs(s.substr(st+,i--st))+"");
  42. }else a.pb("");
  43. st=i+;
  44. }
  45. }
  46. sort(a.begin(),a.end());
  47. rep(i,a.size()) ans=ans+a[i];
  48. return ans;
  49. }
  50.  
  51. int main(){
  52. #ifndef ONLINE_JUDGE
  53. freopen("C.in","r",stdin);
  54. freopen("C.out","w",stdout);
  55. #endif
  56. int T=getint();
  57. while(T--){
  58. cin >> s1 >> s2;
  59. if (dfs(s1)==dfs(s2)) puts("same");
  60. else puts("different");
  61. }
  62. return ;
  63. }
Subway tree systems
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7060   Accepted: 2935

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

  1. 2
  2. 0010011101001011
  3. 0100011011001011
  4. 0100101100100111
  5. 0011000111010101

Sample Output

  1. same
  2. different

Source

[Submit]   [Go Back]   [Status]   [Discuss]

【POJ】【1635】Subway Tree Systems的更多相关文章

  1. poj 1635 Subway tree systems(树的最小表示)

    Subway tree systems POJ - 1635 题目大意:给出两串含有‘1’和‘0’的字符串,0表示向下搜索,1表示回溯,这样深搜一颗树,深搜完之后问这两棵树是不是同一棵树 /* 在po ...

  2. poj-1635 Subway tree systems(推断两个有根树是否同构)-哈希法

    Description Some major cities have subway systems in the form of a tree, i.e. between any pair of st ...

  3. 【树哈希】poj1635 Subway tree systems

    题意:给你两颗有根树,判定是否同构. 用了<Hash在信息学竞赛中的一类应用>中的哈希函数. len就是某结点的子树大小,g是某结点的孩子数+1. 这个值也是可以动态转移的!具体见论文,所 ...

  4. 【 POJ - 1204 Word Puzzles】(Trie+爆搜|AC自动机)

    Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10782 Accepted: 4076 Special ...

  5. 【POJ 1459 power network】

    不可以理解的是,测评站上的0ms是怎么搞出来的. 这一题在建立超级源点和超级汇点后就变得温和可爱了.其实它本身就温和可爱.对比了能够找到的题解: (1)艾德蒙·卡普算法(2)迪尼克算法(3)改进版艾德 ...

  6. 【POJ 2728 Desert King】

    Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 27109Accepted: 7527 Description David the ...

  7. 【POJ 2976 Dropping tests】

    Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 13849Accepted: 4851 Description In a certa ...

  8. 【POJ 3080 Blue Jeans】

    Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 19026Accepted: 8466 Description The Genogr ...

  9. 【POJ各种模板汇总】(写在逆风省选前)(不断更新中)

    1.POJ1258 水水的prim……不过poj上硬是没过,wikioi上的原题却过了 #include<cstring> #include<algorithm> #inclu ...

随机推荐

  1. luigi学习9--执行模型

    luigi的执行和触发模型非常简单. 一.luigi的执行模型 当你执行一个luigi的工作流的时候,worker调度所有的task,并且执行task在一个单独的进程中. 这种scheme最大的好处是 ...

  2. Oozie和Azkaban的技术选型和对比

    1 两种调度工具功能对比图 下面的表格对上述2种hadoop工作流调度器的关键特性进行了比较,尽管这些工作流调度器能够解决的需求场景基本一致,但在设计理念,目标用户,应用场景等方面还是存在区别 特性 ...

  3. echo -n -e参数详解

      echo -n 不换行输出 最终输出  123456 而不是 123 456   echo -e 处理特殊字符   若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出: \a 发出 ...

  4. 利用Newtonsoft.Json实现Json序列化与反序列化

    在项目中用到了Newtonsoft.Json来实现序列化和反序列化,在这里写下实现代码. 1.创建类用于排除不序列化的属性 public class ExcludePropertiesContract ...

  5. 解决matplotlib中文乱码问题(Windows)

    1.修改matplotlibrc文件 进入Python安装目录下的Lib\site-packages\matplotlib\mpl-data目录,打开matplotlibrc文件,删除font.fam ...

  6. python35

    在计算机内存中,统一使用Unicode编码,当需要保存到硬盘或者需要传输的时候,就转换为UTF-8编码.Python对bytes类型的数据用带b前缀的单引号或双引号表示x = b'ABC' //每个字 ...

  7. 分布式缓存Memcached

       分布式缓存服务器,既然用到数据缓存很明显就是想高效性的获取数据,大容量的存储数据.为了可以缓存大量的数据以及可以高效获取数据,那么分布式缓存数据库就要解决数据可以水平线性扩展,这样可以扩大数据容 ...

  8. hdu 5272 Dylans loves numbers

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5272 Dylans loves numbers Description Who is Dylans?Y ...

  9. 九度oj 1528 最长回文子串

    原题链接:http://ac.jobdu.com/problem.php?pid=1528 小白书上的做法,不过这个还要简单些... #include<algorithm> #includ ...

  10. 编写可维护的JavaScript之事件处理

    规则1:隔离应用逻辑 这会让你的代码容易调试 规则2:不要分发事件对象 event对象包含了太多信息 // a good example var handlePopup = { // 事件句柄,处理所 ...