zoukankan      html  css  js  c++  java
  • 51nod 1832 先序遍历与后序遍历(dfs+高精度)

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

    题意:

    思路:

    官方题解如下:

    可以看一下这篇文章:https://wenku.baidu.com/view/a2a45aa0284ac850ad024261.html

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<queue>
      5 using namespace std;
      6 const int maxn = 10000+5;
      7 const int inf = 0x3f3f3f3f;
      8 
      9 int n;
     10 int a[maxn],b[maxn];
     11 
     12 struct BigInt
     13 {
     14     const static int mod = 10000;
     15     const static int DLEN = 4;
     16     int a[600],len;
     17     BigInt()
     18     {
     19         memset(a,0,sizeof(a));
     20         len = 1;
     21     }
     22     BigInt(int v)
     23     {
     24         memset(a,0,sizeof(a));
     25         len = 0;
     26         do
     27         {
     28             a[len++] = v%mod;
     29             v /= mod;
     30         }while(v);
     31     }
     32     BigInt(const char s[])
     33     {
     34         memset(a,0,sizeof(a));
     35         int L = strlen(s);
     36         len = L/DLEN;
     37         if(L%DLEN)len++;
     38         int index = 0;
     39         for(int i = L-1;i >= 0;i -= DLEN)
     40         {
     41             int t = 0;
     42             int k = i - DLEN + 1;
     43             if(k < 0)k = 0;
     44             for(int j = k;j <= i;j++)
     45                 t = t*10 + s[j] - '0';
     46             a[index++] = t;
     47         }
     48     }
     49     BigInt operator +(const BigInt &b)const
     50     {
     51         BigInt res;
     52         res.len = max(len,b.len);
     53         for(int i = 0;i <= res.len;i++)
     54             res.a[i] = 0;
     55         for(int i = 0;i < res.len;i++)
     56         {
     57             res.a[i] += ((i < len)?a[i]:0)+((i < b.len)?b.a[i]:0);
     58             res.a[i+1] += res.a[i]/mod;
     59             res.a[i] %= mod;
     60         }
     61         if(res.a[res.len] > 0)res.len++;
     62         return res;
     63     }
     64     BigInt operator *(const BigInt &b)const
     65     {
     66         BigInt res;
     67         for(int i = 0; i < len;i++)
     68         {
     69             int up = 0;
     70             for(int j = 0;j < b.len;j++)
     71             {
     72                 int temp = a[i]*b.a[j] + res.a[i+j] + up;
     73                 res.a[i+j] = temp%mod;
     74                 up = temp/mod;
     75             }
     76             if(up != 0)
     77                 res.a[i + b.len] = up;
     78         }
     79         res.len = len + b.len;
     80         while(res.a[res.len - 1] == 0 &&res.len > 1)res.len--;
     81         return res;
     82     }
     83     void output()
     84     {
     85         printf("%d",a[len-1]);
     86         for(int i = len-2;i >=0 ;i--)
     87             printf("%04d",a[i]);
     88         printf("
    ");
     89     }
     90 };
     91 BigInt ans;
     92 
     93 void dfs(int al, int ar, int bl, int br)
     94 {
     95     if(ar-al<=0)  return;
     96     al++;
     97     br--;
     98     int cnt = 0;
     99     int index = bl;
    100     while(a[al]!=b[index])  index++;
    101     int newar = al+index-bl+1;
    102     int newbl = index+1;
    103     cnt++;
    104     dfs(al,newar-1,bl,index);
    105     if(ar-al!=index-bl)
    106     {
    107         cnt++;
    108         dfs(newar,ar,newbl,br);
    109     }
    110     if(cnt==1)  ans=ans*2;
    111 }
    112 
    113 int main()
    114 {
    115     //freopen("in.txt","r",stdin);
    116     ans = 1;
    117     scanf("%d",&n);
    118     for(int i=0;i<n;i++)  scanf("%d",&a[i]);
    119     for(int i=0;i<n;i++)  scanf("%d",&b[i]);
    120     dfs(0,n-1,0,n-1);
    121     ans.output();
    122     return 0;
    123 }
  • 相关阅读:
    sql server分布式事务解决方案 (笔记)
    深入理解Datatable(笔记)
    SQLSERVER 2005 实现文件读写操作
    局部变量、全局变量、堆、堆栈、静态和全局
    分布式事务DISTRIBUTED TRANSACTION
    SQLSEVER2005中找不到存储过程xp_getfiledetails 解决办法
    模式窗口
    理解析构函数的执行过程
    Oracle分页
    DataTable导出到Excel、GSV (转)
  • 原文地址:https://www.cnblogs.com/zyb993963526/p/8039550.html
Copyright © 2011-2022 走看看