1. 程式人生 > >poj 1780 Code(歐拉回路+鼓輪模型)

poj 1780 Code(歐拉回路+鼓輪模型)

Code
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1645 Accepted: 625

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 several models available, from toy safes for children (with a 2-digit code) to the military version (with a 6-digit code). 

The safe will open as soon as the last digit of the correct code is entered. There is no "enter" key. When you enter more than n digits, only the last n digits are significant. For example (in the 4-digit version), if the correct code is 4567, and you plan to enter the digit sequence 1234567890, the door will open as soon as you press the 7 key. 

The software to create this effect is rather simple. In the n-digit version the safe is always in one of 10n-1
 internal states. The current state of the safe simply represents the last n-1 digits that have been entered. One of these states (in the example above, state 456) is marked as the unlocked state. If the safe is in the unlocked state and then the right key (in the example above, 7) is pressed, the door opens. Otherwise the safe shifts to the corresponding new state. For example, if the safe is in state 456 and then you press 8, the safe goes into state 568. 

A trivial strategy to open the safe is to enter all possible codes one after the other. In the worst case, however, this will require n * 10n
 keystrokes. By choosing a good digit sequence it is possible to open the safe in at most 10n + n - 1 keystrokes. All you have to do is to find a digit sequence that contains all n-digit sequences exactly once. KEY Inc. claims that for the military version (n=6) the fastest computers available today would need billions of years to find such a sequence - but apparently they don't know what some programmers are capable of...

Input

The input contains several test cases. Every test case is specified by an integer n. You may assume that 1<=n<=6. The last test case is followed by a zero.

Output

For each test case specified by n output a line containing a sequence of 10n + n - 1 digits that contains each n-digit sequence exactly once.

Sample Input

1
2
0

Sample Output

0123456789
00102030405060708091121314151617181922324252627282933435363738394454647484955657585966768697787988990

Source


思路:要想最短,那麼每個相鄰的N位數前一個的後N-1位就是後一個的前N-1位。有要求序列字典序最小。因此使用棧從小到大搜。

#include<iostream>
#include<cstring>
using namespace std;
const int mm=1000109;
int g[mm],stak[mm],pos;///g[x](N-1位)代表x出現了多少次
char ans[mm];
int n;
void see(int v,int m)///找出v為開頭最小後序
{ int w;
  while(g[v]<10)
  {
    w=v*10+g[v];++g[v];
    v=w%m;stak[pos++]=w;
  }
}
int main()
{ int m,v,z;
  while(cin>>n&&n)
  { m=1;pos=0;
    memset(g,0,sizeof(g));
    memset(stak,0,sizeof(stak));
    for(int i=1;i<n;i++)m*=10;
    v=0;
    see(v,m);
    z=0;
    while(pos)
    {
      v=stak[--pos];ans[z++]=v%10+'0';
      v/=10;see(v,m);
    }
    for(int i=1;i<n;i++)cout<<"0";
    for(int i=z-1;i>=0;i--)
      cout<<ans[i];
    cout<<"\n";
  }
}


相關推薦

poj 1780 Code(+模型)

Code Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1645 Accepted: 625 Description KEY Inc., the leading company in s

[] poj 1300 Door Man

linker center || 是否 connect sep cto -m vector 題目鏈接: http://poj.org/problem?id=1300 Door Man Time Limit: 1000

POJ-2513 Colored Sticks---+並查集+字典樹

lan [] 復雜 題意 沒有 fin pre else 解題思路 題目鏈接: https://vjudge.net/problem/POJ-2513 題目大意: 給一些木棍,兩端都有顏色,只有兩根對應的端點顏色相同才能相接,問能不能把它們接成一根木棍 解題思路: 題意不難

求路徑POJ 2230

possible 圖片 pac ans close rails AI show pair Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8841

BZOJ3033: 太達人(

Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 524  Solved: 400[Submit][Status][Discuss] Description   七夕祭上

POJ 1041 John's trip(+輸出路徑)

題意:         給你一個無向圖,資料格式如點x 點y 邊Z,表示由x點和y點構成了Z邊。現在要問你該圖中是否存在歐拉回路,如果存在,則輸出字典序最小的那條歐拉回路(輸入按序走過的所有邊標號)。且題目中保證了該無向圖是連通的。 別的博文有一些方法

poj 1637 Sightseeing tour —— 最大流+

題目:http://poj.org/problem?id=1637 建圖很妙; 先給無向邊隨便定向,這樣會有一些點的入度不等於出度; 如果入度和出度的差值不是偶數,也就是說這個點的總度數是奇數,那麼一定無解; 隨便定向後,如果定向 x -> y,那麼從 y 向 x 連一條容量為1的邊,將來選了這

poj 2513 +並查集判斷是否聯通+Trie樹

http://poj.org/problem?id=2513 最初看到 第一感覺---map  一看250000的資料量 果斷放棄 然後記得以前看過,trie代替map,尤其當資料量特別大的時候 學到了: 1、Trie代替map的思想,可以在單詞結尾的tree[i][tk]

POJ 1041 無向圖的

John's trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6569 Accepted: 2182 Special Judge Description Little Johnny h

Poj 1637 Sightseeing tour (混合圖的判定)

題意:給出一個混合圖,要求判定歐拉回路是否存在,輸入 x y d,如果d為0則為無向邊,為1則為有向邊。 首先應該判定圖的連通性!本題所給圖均是連通的,所以沒有判斷。 對所有的無向邊隨便定向,之後再進行調整。 統計每個點的出入度,如果有某個點出入度之差為奇數,則不存在歐

poj 1637 判斷混合圖是否存在

題意: 就是對有無向邊和有向邊的混合圖,判斷存不存在歐拉回路。  若圖G中存在這樣一條路徑,使得它恰通過G中每條邊一次,則稱該路徑為尤拉路徑。若該路徑是一個圈,則稱為尤拉(Euler)迴路。 參考下面的解釋: 【混合圖】 混合圖(既有有向邊又有無向邊的圖)中尤拉環、尤拉路

put ++ .cn -1 bool ret 技術分享 代碼 can 思路 根據歐拉圖的概念來。 註意 點數為1; 有孤立點; 代碼實現 T掉的dfs... 1 #include<cstdio> 2 const int max

UVA 10196 Morning Walk(

ble move eve man first pre intersect sum ons Problem H Morning Walk Time Limit 3 Seconds Kamalis a Motashotaguy. He has

hdu1878(DFS+)

out sin 整數 white 偶數 ret pad bottom -m 歐拉回路 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

[模板][持續更新]路徑淺析

bits solution 算法 -1 要求 logs 鏈式前向星 namespace src Luogu P2731 騎馬修柵欄 Riding the Fences 題目背景 Farmer John每年有很多柵欄要修理。他總是騎著馬穿過每一個柵欄並修復它破損的地方。 題目

51nod 1967 路徑定向(不錯的

cnblogs 偶數 ret mes stack ostream lin .html pre http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1967 題意: 思路: 出度=入度,這

The Necklace UVA - 10054 (無向圖的

n) 兩個 logs nec get dfs lap none view The Necklace UVA - 10054 題意:每個珠子有兩個顏色,給n個珠子,問能不能連成一個項鏈,使得項鏈相鄰的珠子顏色相同。 把顏色看做點,珠子內部連一條邊,無向圖求歐拉回路。 這

算法復習——混合圖(bzoj2095二分+網絡流)

n) truct lin 歐拉圖 所有 mage borde algo stream 題目: Description YYD為了減肥,他來到了瘦海,這是一個巨大的海,海中有n個小島,小島之間有m座橋連接,兩個小島之間不會有兩座橋,並且從一個小島可以到另外任意一個小島。現在

混合圖

所有 ace .cn geo urn 我們 方向 online n) http://acm.pku.edu.cn/JudgeOnline/problem?id=1273 給一組邊 有的是有向邊有的是無向邊 問是否存在歐拉回路 我們知道如果每個點入度等於出度 就存在歐拉回路

hdu 1956 (網絡流解決

www 起點到終點 更改 什麽 tps 網絡流 個性 http 混合圖 題目連接:https://vjudge.net/problem/HDU-1956 題意:給定一些點和一些邊,有些邊是有向的,,有些邊是無向的,求是否存在歐拉回路。 題解:想不到的網絡流。 混合圖:即