1. 程式人生 > 其它 >一些測試報告模板

一些測試報告模板

題目傳送門

一、已知後序+中序,求前序

https://www.cnblogs.com/littlehb/p/15088998.html

二、已知前序+中序,求後序

https://www.cnblogs.com/littlehb/p/15088448.html

三、已知前序+後序,求中序個數

只有一個兒子的節點 才會在知道 前序後序的情況下有不同的中序遍歷,所以將題目轉化成找只有一個兒子的節點個數。

可以很容易的找出這類節點在前序後序中出現的規律。(前序中出現AB,後序中出現BA,則這個節點只有一個兒子)

每個這類節點有兩種中序遍歷(及兒子在左,兒子在右)根據乘法原理中序遍歷數為 2^節點個數種

#include <bits/stdc++.h>

using namespace std;
const int N = 100010;
int ans = 1;//最少一種中序排列
string a, b;


int main() {
    cin >> a >> b;
    for (int i = 0; i < a.size() - 1; i++) {
        string t = a.substr(i, 2); // 擷取相鄰的兩個字串
        reverse(t.begin(), t.end()); // 反轉
        if (b.find(t) != string::npos) ans *= 2; // 匹配成功則總數*2
    }
    cout << ans << endl;
    return 0;
}