盲僧背后有根树背后痒是怎么回事事?

树有根水有源下一句是什么_百度知道
树有根水有源下一句是什么
我有更好的答案
现在写实的情景应该是,故其流不穷,故不见绿叶,那是反映的过去;木有根;木有根,故其生不穷 胡宏《知言》(四)之《好恶篇》 上述说法。 原自《瞎编》之环境污染篇: 水有源,故污水横流水有源
水有源,树有根,儿女不忘养育恩。
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁无根树和有根树有什么根本区别?_百度知道
无根树和有根树有什么根本区别?
我有更好的答案
无根树是指没有安定的心,在张的无根树,指人心
其他类似问题
为您推荐:
无根树的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁poj-1635 Subway tree systems(判断两个有根树是否同构)-哈希法
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.&
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.
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
Sample Output
其实题目意思就是求两个有根树是否同构,这个如果暴力法枚举的话,复杂度是O(N^2),一中经典的做法是哈希,思想就是使得不同结构的树哈希值不同,而同构的树哈希值相同。
我这个也是参考别人写的,不同的是我先把01串转换为了树状结构表示,然后再递归求哈希值,这样好理解一点。
哈希的策略:先随机产生一系列随机数作为存到数组,接着从根节点出发,递归计算每个子树的哈希值,将子树的哈希值相加然后和父节点自己对应的数组上的随机数相加得到父节点的哈希值。这个计算结果和子树的顺序是没有关系的,所以同构的树一哈希值一定是一样的。对于异构的树,必然在某些节点计算的哈希值不同,由于都是随机产生的一些数字,所以他们相加值和另外一棵树哈希值相同的概率也会非常低。(应该不能完全保证的,这里我们加了个取模m的操作,根据鸽巢原理,当树的数量超过m,必然有两个树的哈希值是会相同的,但这两个树却未必是同构的,不知道大家觉得对不对?)
import java.util.*; &
public class SubwayTreeSstems1635 { &
& & static final int Hn=11000; &
& & static int h[]=new int[Hn]; &
& & static Random rand=new Random(System.currentTimeMillis()); &
& & static int m=; &
& & static int index=0; &
& & &* @param args&
& & public static void main(String[] args) { &
& & & & run(); &
& & private static void init() { &
& & & & for(int i=0;i&Hn;i++) &
& & & & & & h[i]=(rand.nextInt()%m); &
& & public static void run() &
& & & & Scanner in=new Scanner(System.in); &
& & & & int T=in.nextInt(); &
& & & & init(); &
& & & & for(int t=0;t&T;t++) &
& & & & { &
& & & & & & String s1=in.next(); &
& & & & & & Node tree1=createTree(s1); &
& & & & & & String s2=in.next(); &
& & & & & & Node tree2=createTree(s2); &
& & & & & & /*System.out.println(tree1.children.size()+& &+tree2.children.size());&
& & & & & & displayTree(tree1);&
& & & & & & System.out.println();&
& & & & & & displayTree(tree2);*/ &
& & & & & & &&
& & & & & & int a=hash(tree1,1); &
& & & & & & int b=hash(tree2,1); &
& & & & & & //System.out.println(a+& &+b); &
& & & & & & if(a==b) &
& & & & & & { &
& & & & & & & & System.out.println(&same&); &
& & & & & & } &
& & & & & & else &
& & & & & & { &
& & & & & & & & System.out.println(&different&); &
& & & & & & } &
& & & & } &
& & public static int hash(Node tree,int j) &
& & & & int sum=h[j+5000];//j是树的高度 &
& & & & for(Node n:tree.children) &
& & & & & & sum=(sum+h[j]*hash(n,j+1))%m;//把子树的哈希值加到父节点上去 &
& & & & return (sum*sum)%m; &
& & & & &&
& & private static Node createTree(String s) { &
& & & & char[] seq=s.toCharArray(); &
& & & & Node root=new Node(0); &
& & & & Node p= &
& & & & int index=1; &
& & & & for(int i=0;i&seq.i++) &
& & & & { &
& & & & & & if(seq[i]=='0') &
& & & & & & { &
& & & & & & & & Node node =new Node(index++); &
& & & & & & & & connect(p,node); &
& & & & & & & & p= &
& & & & & & } &
& & & & & & else if(seq[i]=='1') &
& & & & & & { &
& & & & & & & & p=p. &
& & & & & & } &
& & & & } &
& & & & //if(p==root) &
& & & & // &System.out.println(&create success!&); &
& & private static void connect(Node p, Node node) { &
& & & & node.parent=p; &
& & & & p.children.add(node); &
& & public static void displayTree(Node tree) &
& & & & System.out.println(tree); &
& & & & for(Node ch:tree.children) &
& & & & & & displayTree(ch); &
class Node &
& & Node parent= &
& & List&Node& children=new ArrayList&Node&(); &
& & public Node(int n) &
& & & & id=n; &
& & public String toString() &
& & & & StringBuilder sb=new StringBuilder(); &
& & & & sb.append(id).append(&: &); &
& & & & for(Node n:children) &
& & & & & & sb.append(n.id).append(& &); &
& & & & return sb.toString(); &
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'&&国之画&&&& &&&&&&
&& &&&&&&&&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生!

我要回帖

更多关于 背后疼痛是怎么回事 的文章

 

随机推荐