51nod1832(二叉樹/高精度模板+dfs)

題目鏈接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1832

?

題意: 中文題誒~

?

思路: 若二叉樹中有 k 個節點只有一個子樹, 則答案為 1 << k.

詳情參見:http://blog.csdn.net/gyhguoge01234/article/details/77836484

?

代碼:

  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <string.h>
  4 #define ll long long
  5 using namespace std;
  6 
  7 const int MAX = 1e2;
  8 const int M = 1e9;//1e9為1節
  9 const int MAXN = 35;
 10 
 11 struct BigInt{
 12     const static int mod = 10000;
 13     const static int DLEN = 4;
 14     int a[600], len;
 15     BigInt(){
 16         memset(a, 0, sizeof(a));
 17         len = 1;
 18     }
 19     BigInt(int v){
 20         memset(a, 0, sizeof(a));
 21         len = 0;
 22         do{
 23             a[len++] = v % mod;
 24             v /= mod;
 25         }while(v);
 26     }
 27     BigInt(const char s[]){
 28         memset(a, 0, sizeof(a));
 29         int L = strlen(s);
 30         len = L / DLEN;
 31         if(L % DLEN) len++;
 32         int index = 0;
 33         for(int i = L - 1; i >= 0; i -= DLEN){
 34             int t = 0;
 35             int k = i - DLEN + 1;
 36             if(k < 0) k = 0;
 37             for(int j = k; j <= i; j++)
 38                 t = t * 10 + s[j] - '0';
 39             a[index++] = t;
 40         }
 41     }
 42     BigInt operator +(const BigInt &b)const{
 43         BigInt res;
 44         res.len = max(len, b.len);
 45         for(int i = 0; i <= res.len; i++) res.a[i] = 0;
 46         for(int i = 0; i < res.len; i++){
 47             res.a[i] += ((i < len) ? a[i] : 0) + ((i < b.len) ? b.a[i] : 0);
 48             res.a[i + 1] += res.a[i] / mod;
 49             res.a[i] %= mod;
 50         }
 51         if(res.a[res.len] > 0) res.len++;
 52         return res;
 53     }
 54     BigInt operator *(const BigInt &b)const{
 55         BigInt res;
 56         for(int i = 0; i < len; i++){
 57             int up = 0;
 58             for(int j = 0; j < b.len; j++){
 59                 int temp = a[i] * b.a[j] + res.a[ i + j] + up;
 60                 res.a[i + j] = temp%mod;
 61                 up = temp / mod;
 62             }
 63             if(up != 0)
 64             res.a[i + b.len] = up;
 65         }
 66         res.len = len + b.len;
 67         while(res.a[res.len - 1] == 0 && res.len > 1) res.len--;
 68         return res;
 69     }
 70     void output(){
 71         printf("%d", a[len - 1]);
 72         for(int i = len - 2; i >= 0; i--)
 73             printf("%04d", a[i]);
 74         printf("\n");
 75     }
 76 };
 77 
 78 // 先序遍歷 X L … R …
 79 // 后序遍歷 … L … R X
 80 
 81 const int N = 1e5 + 10;
 82 int a[N], b[N];
 83 BigInt sol(1);
 84 
 85 void dfs(int al, int ar, int bl, int br){
 86     if(ar - al <= 1) return;
 87     al++;
 88     br--;
 89     int indx = bl, cnt = 0;;
 90     while(a[al] != b[indx]) indx++;
 91     int newar = al + (indx - bl + 1);
 92     int newbr = indx + 1;
 93     cnt++;
 94     dfs(al, newar, bl, newbr);
 95     if(ar - al != indx - bl + 1){
 96         cnt++;
 97         dfs(newar, ar, newbr, br);
 98     }
 99     if(cnt == 1) sol = sol * 2;
100 }
101 
102 int main(void){
103     int n;
104     scanf("%d", &n);
105     for(int i = 0; i < n; i++){
106         scanf("%d", &a[i]);
107     }
108     for(int i = 0; i < n; i++){
109         scanf("%d", &b[i]);
110     }
111     sol = 1;
112     dfs(0, n, 0, n);
113     sol.output();
114     return 0;
115 }
View Code

?

轉載于:https://www.cnblogs.com/geloutingyu/p/7693105.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391076.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391076.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391076.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

重學TCP協議(11)TFO(Tcp Fast Open)

1. TFO 為了改善web應用相應時延&#xff0c;google發布了通過修改TCP協議利用三次握手時進行數據交換的TFO(TCP fast open&#xff0c;RFC 7413)。 TFO允許在TCP握手期間發送和接收初始SYN分組中的數據。如果客戶端和服務器都支持TFO功能&#xff0c;則可以減少建立到同一服…

[網絡安全] 遠程登錄

遠程登錄方式: 1.圖像化遠程登錄 做法: 運行"窗口"輸入 "mstsc " 輸入ip地址 注意: 被遠程計算機&#xff0c;必須打開遠程登錄服務: 信息面板–系統–允許遠程訪問。被遠程計算機&#xff0c;必須存在擁有遠程桌面權限的用戶。 2.命令行遠程登錄 teln…

外星人圖像和外星人太空船_衛星圖像:來自太空的見解

外星人圖像和外星人太空船By Christophe Restif & Avi Hoffman, Senior Software Engineers, Crisis Response危機應對高級軟件工程師Christophe Restif和Avi Hoffman Editor’s note: In 2019, we piloted a new feature in Search SOS Alerts for major California wild…

chrome恐龍游戲_如何玩沒有互聯網的Google Chrome恐龍游戲-在線和離線

chrome恐龍游戲Several years ago, Google added a fun little Easter egg to Chrome: if your internet went down and you tried to visit a web page, youd see the message "Unable to connect to the Internet" or "No internet" with a little pixi…

Hotpatch潛在的安全風險

屎蛋 2016/06/22 10:11author:[email protected]0x00 “Hotpatch”簡介IOS App的開發者們經常會出現這類問題&#xff1a;當一個新版本上線后發現存在一個嚴重的bug&#xff0c;有可能因為一個邏輯問題導致支付接口存在被薅羊毛的風險&#xff0c;這個時候能做的只能是趕快修復…

spring中@Inject和@Autowired的區別?分別在什么條件下使用呢?

問題&#xff1a;spring中Inject和Autowired的區別&#xff1f;分別在什么條件下使用呢&#xff1f; 我在瀏覽SpringSource上的一些博客&#xff0c;在其他一個博客中&#xff0c;那個作者用了Inject&#xff0c;但是我覺得他用Autowired也行 下面是一部分代碼&#xff1a; …

Objective-C語言的動態性

Objective-C具有相當多的動態特性&#xff0c;基本的&#xff0c;也是經常被提到和用到的有動態類型&#xff08;Dynamic typing&#xff09;&#xff0c;動態綁定&#xff08;Dynamic binding&#xff09;和動態加載&#xff08;Dynamic loading&#xff09; 一、編譯時和運行…

內存泄漏和內存溢出的區別

原文地址https://www.zhihu.com/question/40560123 簡單來說&#xff0c;操作系統就像資源分配人員&#xff0c;你要使用內存的時候分給你&#xff0c;你用完了還給它。如果你使用了沒有分配給你的內存就是內存溢出&#xff0c;如果你用完了沒有還就是內存泄漏。會引起的問題&a…

怎么注銷筆記本icloud_如何在筆記本電腦或臺式機的Web瀏覽器中在線查看Apple iCloud照片

怎么注銷筆記本icloudPicture this: you just returned from a beautiful vacation and want to show all those gorgeous photos to your family. But your phone just died. And since youre at a family dinner your laptop is nowhere to be found.想象一下&#xff1a;您剛…

棒棒糖 宏_棒棒糖圖表

棒棒糖 宏AKA: lollipop plot又名&#xff1a;棒棒糖情節 WHY: a lollipop chart (LC) is a handy variation of a bar chart where the bar is replaced with a line and a dot at the end. Just like bar graphs, lollipop plots are used to make comparisons between diff…

ubuntu上如何安裝tomcat

1. 在官網下載linux里面的tomcat 2. 放到DownLoads下面--把tomcat的壓縮包放到DownLoads3. sudo mkdir /usr/local/tomcat/ -在usr/local/路徑下新建一個tomcat的文件夾4 sudo tar zxvf tomcat。。。。tar.gz -C /usr/local/tomcat/---把解壓后的tomcat放到usr/local/下的tomca…

leetcode 1734. 解碼異或后的排列(位運算)

給你一個整數數組 perm &#xff0c;它是前 n 個正整數的排列&#xff0c;且 n 是個 奇數 。 它被加密成另一個長度為 n - 1 的整數數組 encoded &#xff0c;滿足 encoded[i] perm[i] XOR perm[i 1] 。比方說&#xff0c;如果 perm [1,3,2] &#xff0c;那么 encoded [2,…

ZooKeeper3.4.5-最基本API開發

2019獨角獸企業重金招聘Python工程師標準>>> package cn.itcast.bigdata.zk;import java.io.IOException; import java.util.List;import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEven…

字符串轉換整數python_將Python字符串轉換為Int:如何在Python中將字符串轉換為整數

字符串轉換整數pythonUnlike many other programming languages out there, Python does not implicitly typecast integers (or floats) to strings when you concatenate them to strings.與現有的許多其他編程語言不同&#xff0c;Python在將整數連接到字符串時不會隱式地將…

理解Java里面的必檢異常和非必檢異常

問題&#xff1a;理解Java里面的必檢異常和非必檢異常 Joshua Bloch在"Effective Java"里面說過 在可恢復的條件下和編程錯誤導致的運行時錯誤時&#xff0c;使用必檢異常&#xff08;第二版的第52頁&#xff09; 讓我們來看一下我對這個的正確理解吧 下面是我對…

使用vim打開文件的16進制形式,編輯和全文替換

1、先用vim打開文件的二進制形式&#xff0c;如果不以二進制可能會產生轉換錯誤。 vim -b file-to-open.dat 2、用xxd把文件轉換成十六進制格式 :%!xxd 現在就可以對待普通文本一樣查看和編輯二進制文件了。 3、vim 單文件替換方法 :%s/old/new/gc 全文執行替換,詢問是…

nlp自然語言處理_不要被NLP Research淹沒

nlp自然語言處理自然語言處理 (Natural Language Processing) 到底是怎么回事&#xff1f; (What is going on?) NLP is the new Computer VisionNLP是新的計算機視覺 With enormous amount go textual datasets available; giants like Google, Microsoft, Facebook etc have…

opencv 隨筆

裝環境好累&#xff0c;python3.6&#xff0c;opencv3.4 好不容易裝好了&#xff0c;結果 addweight的時候總是報錯 The operation is neither array op array (where arrays have the same size and the same number of channels), nor array op scalar, nor scalar op array …

js打開飛行模式_什么是飛行模式? 它有什么作用?什么時候應該打開它?

js打開飛行模式If youve flown on an airplane in the last decade and you have a smart phone, youve likely had to put that phone in airplane mode before the plane takes off.如果您在過去的十年中乘坐過飛機&#xff0c;并且擁有一部智能手機&#xff0c;那么您可能必…

在Java 里面怎么比較字符串

問題&#xff1a;在Java 里面怎么比較字符串 到目前為止&#xff0c;我使用 操作符去比較字符串在我的程序里面。然而&#xff0c;卻產生了一個bug&#xff0c;將這個改為了.equals()以后&#xff0c;就把bug修復了 是不是太辣雞了&#xff1f;它什么時候應該被使用或者說是不…