• poj 1780 , poj 1392 欧拉回路求前后相互衔接的数字串


    两道题目意思差不多

    第一题是10进制 , 第二题是2进制的

    都是利用欧拉回路的fleury算法来解决

    因为我总是希望小的排在前面,所以我总是先将较小数加入栈,再利用另一个数组接收答案,但是这里再从栈中导出来答案要倒一下了,这一点要注意

    poj 1780

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <cmath>
     4 using namespace std;
     5 #define N 1000010
     6 
     7 int ans[N] , cnt[N] , stack[N];
     8 int top1 , top2;
     9 int mod;
    10 void euler(int v)
    11 {
    12     while(cnt[v]<10)
    13     {
    14         int w=v*10+cnt[v];
    15         cnt[v]++;
    16         stack[top1++]=w;
    17         v=w%mod;
    18     }
    19 }
    20 
    21 int main()
    22 {
    23   //  freopen("in.txt" , "r" , stdin);
    24     int n;
    25     while(scanf("%d" , &n) , n)
    26     {
    27         top1 = 0 , top2 = 0 , mod = (int)pow(10.0,(n-1)*1.0);
    28         stack[top1++] = 0;
    29         memset(cnt , 0 , sizeof(cnt));
    30         cnt[0]++;
    31         while(top1)
    32         {
    33             ans[top2++] = stack[--top1];
    34             int v = ans[top2-1]/10;
    35             euler(v);
    36         }
    37         for(int i=1 ; i<=n ; i++) printf("%d" , 0);
    38         for(int i=top2-1 ; i>=1 ; i--) printf("%d" , ans[i]%10);
    39         puts("");
    40     }
    41     return 0;
    42 }

    poj 1392

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 
     5 using namespace std;
     6 #define N (1<<16)
     7 
     8 int ans[N] , cnt[N] , stack[N];
     9 int top1 , top2 , mod;
    10 
    11 void euler(int v , int mod)
    12 {
    13     while(cnt[v]<2)
    14     {
    15         int w=v*2+cnt[v];
    16         cnt[v]++;
    17         stack[top1++]=w;
    18         v=w%mod;
    19     }
    20 }
    21 
    22 int q_pow(int a , int b)
    23 {
    24     int ans = 1;
    25     while(b)
    26     {
    27         if(b&1) ans *= a;
    28         a*=a;
    29         b>>=1;
    30     }
    31     return ans;
    32 }
    33 
    34 int main()
    35 {
    36    // freopen("in.txt" , "r" , stdin);
    37     int n,k;
    38     while(scanf("%d%d" , &n , &k) , n||k)
    39     {
    40         top1=top2=0;
    41         memset(cnt , 0 , sizeof(cnt));
    42         stack[top1++] = 0;
    43         mod = q_pow(2 , n-1);
    44         while(top1)
    45         {
    46             ans[top2++] = stack[--top1];
    47             int v=stack[top1]/2;
    48             euler(v , mod);
    49         }
    50         int index = top2-k-1;
    51         printf("%d
    " , ans[index]);
    52     }
    53     return 0;
    54 }
  • 相关阅读:
    【leetcode】Binary Search Tree Iterator
    【leetcode】Palindrome Partitioning II
    【leetcode】Best Time to Buy and Sell Stock III
    【leetcode】Best Time to Buy and Sell Stock II
    【leetcode】Longest Consecutive Sequence
    【leetcode】Factorial Trailing Zeroes
    【leetcode】Simplify Path
    【leetcode】Generate Parentheses
    【leetcode】Combination Sum II
    【leetcode】Combination Sum
  • 原文地址:https://www.cnblogs.com/CSU3901130321/p/4467807.html
Copyright © 2020-2023  润新知