zoukankan      html  css  js  c++  java
  • 洛谷 p1030 求先序排列

    这个题做了快半上午

    实际上只是一道普及-(因为我太蒻了)

    我不太理解递归啊,我实在太蒻了,于是就开始做了这道题

    一开始数组越界,蜜汁RE


    吐槽结束

    思路大家应该都明白吧

    找后序遍历的最后一个,去中序遍历找到,然后左右二分,就这么简单

    #include<bits/stdc++.h>
    
    using namespace std;
    
    string a,b;
    
    void sg(int a1,int a2,int b1,int b2){//a1,b1是两个遍历的起点,a2,b2是终点
    	cout<<b[b2];//输出后序遍历最后一个
    	int x;//x记录中序遍历中后序最后一个的位置
    	for (int i = a1; i <= a2; ++i)//好像也可以用什么什么"邦德",咱也不会读,咱也不会拼,我这种蒟蒻怎么会这种东西
    		if(a[i] == b[b2]){
    			x = i;
    			break;
    		}
    	if(a1 <= x - 1 && b1 <= b1 + x - 1 - a1)//越界就不递归
    		sg(a1,x - 1,b1,b1 + x - 1 - a1);//左边,为什么这样拿样例自己分一下就知道了
    	if(x + 1 <= a2 && b1 + x - a1 <= b2 - 1)
    		sg(x + 1,a2,b1 + x - a1,b2 - 1);//右边
    }
    
    int main(int argc, char const *argv[])
    {
    	cin>>a>>b;
    	int lea,leb;
    	lea = a.length() - 1;
    	leb = b.length() - 1;
    	sg(0,lea,0,leb);//起始长度
    	return 0;
    }//下面是一组自己的数据,正确答案是125763i89,大家可以写完之后测试一下
    //75261i389
    //7562i9831
    
    
  • 相关阅读:
    Sum Root to Leaf Numbers
    Sum Root to Leaf Numbers
    Sort Colors
    Partition List
    Binary Tree Inorder Traversal
    Binary Tree Postorder Traversal
    Remove Duplicates from Sorted List II
    Remove Duplicates from Sorted List
    Search a 2D Matrix
    leetcode221
  • 原文地址:https://www.cnblogs.com/lztzs/p/11088941.html
Copyright © 2011-2022 走看看