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\n" , ans[index]);
52     }
53     return 0;
54 }
时间: 2024-08-02 15:11:10

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

POJ - 1780 Code (欧拉回路+手写DFS)

Description KEY Inc., the leading company in security hardware, has developed a new kind of safe. To unlock it, you don't need a key but you are required to enter the correct n-digit code on a keypad (as if this were something new!). There are severa

求两个字符串(数字串也是一样)是不是循环同构

第一种方法是kmp:将一个数组复制一次,然后再用另一个进行匹配. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 200005; const int MOD = 360000; int a[N],b[N],c[2*N],d[N],f[N]; void getFail(int n){

POJ 1780 Code (欧拉回路+非递归版dfs)

题目地址:POJ 1780 还是求序列的欧拉回路.只不过这题有两坑. 第一坑是用数字来当点的话,会MLE,因为每个数字可以连10条边,100w条边会MLE,即使用vector也会TLE.这题可以用边来记录,对于n为1时直接输出,然后后面的,比如12,23这两个点就用边权值为123来表示这两个点,这样就把点和边的范围都缩小了10倍. 第二坑是用递归的dfs会爆栈,亲测即使加手扩栈也会爆..(简直丧心病狂..)需要用非递归版dfs,也不难,dfs本身就是利用的栈,所以改成栈的形式就可以了. 代码如下

POJ 1780 Code 欧拉回路+手写栈DFS

和西安邀请赛那道题题目差不多,现在终于会手写栈了,自己琢磨了好久,真是感动TAT #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdli

【POJ 1408】 Fishnet (叉积求面积)

[POJ 1408] Fishnet (叉积求面积) 一个1*1㎡的池塘 有2*n条线代表渔网 问这些网中围出来的最大面积 一个有效面积是相邻两行和相邻两列中间夹的四边形 Input为n 后面跟着四行 每行n个浮点数 每一行分别代表a,b,c,d 如图 并且保证a(i) > a(i-1) b(i) > b(i-1) c(i) > c(i-1) d(i) > d(i-1) n(n <= 30)*2+4(四个岸)条边 枚举点数就行 相邻的四个四个点枚举 找出围出的最大面积 找点用

poj 1265 Area (Pick定理+求面积)

链接:http://poj.org/problem?id=1265 Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4969   Accepted: 2231 Description Being well known for its highly innovative products, Merck would definitely be a good target for industrial espionag

POJ 2774 后缀数组:求最长公共子串

思路:其实很简单,就是两个字符串连接起来,中间用个特殊字符隔开,然后用后缀数组求最长公共前缀,然后不同在两个串中,并且最长的就是最长公共子串了. 注意的是:用第一个字符串来判断是不是在同一个字符中,刚开始用了第二个字符的长度来判断WA了2发才发现. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #include<

poj 1226 hdu 1238 Substrings 求若干字符串正串及反串的最长公共子串 2002亚洲赛天津预选题

题目:http://poj.org/problem?id=1226 http://acm.hdu.edu.cn/showproblem.php?pid=1238 其实用hash+lcp可能也可以,甚至可能写起来更快,不过我没试,我最近在练习后缀数组,所以来练手 后缀数组的典型用法之一----------------后缀数组+lcp+二分 思路:1.首先将所有的字符串每读取一个,就将其反转,作为一组,假设其下标为i到j,那么cnt[i]到cnt[j]都标记为一个数字(这个数字意思是第几个读入的字符

POJ 3177 边双连通求连通量度的问题

这道题的总体思路就是找到连通量让它能够看作一个集合,然后找这个集合的度,度数为1的连通量为k,那么需要添加(k+1)/2条边才可以保证边双连通 这里因为一个连通量中low[]大小是相同的,所以我们用ans[low[i]]++来计度数 这道题我最开始按学长的模板来写....MLE到哭了,也不知道这道题为什么这么逗,把5000的数组改成1000也能过,当然后来换了别的思路 为了防止重边的进入,开始设置了一个hash[][]二维数组来判断边是否已经存在,不额外添入 之后,我不采用二维数组,而是在get