新聞| | PChome| 登入
2012-05-04 11:16:27| 人氣2,853| 回應14 | 上一篇 | 下一篇

[UVA] 10063 - Knuth's Permutation

推薦 0 收藏 0 轉貼0 訂閱站台

Problem I


Knuth’s Permutation


Input: standard input

Output: standard output

 

 

There are some permutation generation techniques in Knuth’s book “The Art of Computer Programming - Volume 1”. One of the processes is as follows:

           For each permutation A1, A2, …, An-1 form n others by inserting a character n in all possible places obtaining

           n A1 A2 … An-1,         A1 n A2 … An-1,        ….,          A1 A2 …n An-1,       A1 A2, … An-1 n

 

           For example, from the permutation 231 inserting 4 in all possible places we get 4231 2431 2341 2314

 

Following this rule you have to generate all the permutation for a given set of characters. All the given characters will be different and there number will be less than 10 and they all will be alpha numerals. This process is recursive and you will have to start recursive call with the first character and keep inserting the other characters in order. The sample input and output will make this clear. Your output should exactly mach the sample output for the sample input.

 

Input

The input contains several lines of input. Each line will be a sequence of characters. There will be less than ten alphanumerals in each line. The input will be terminated by End of File.

 

Output

 For each line of input generate the permutation of those characters. The input ordering is very important for the output. That is the permutation sequence for abc and bca will not be the same. Seperate each set of permutation output with a blank line.

 

Sample Input:

abc
bca
dcba

Sample Output:

cba
bca
bac
cab
acb
abc

acb
cab
cba
abc
bac
bca

abcd
bacd
bcad
bcda
acbd
cabd
cbad
cbda
acdb
cadb
cdab
cdba
abdc
badc
bdac
bdca
adbc
dabc
dbac
dbca
adcb
dacb
dcab
dcba



#include <stdio.h>
#include <string.h>
char str[20], ans[20], len;
void dfs(int idx) {
    if(idx == len) {
        ans[idx] = '\0';
        puts(ans);
        return;
    }
    int i;
    for(i = idx; i >= 1; i--)
        ans[i] = ans[i-1];
    for(i = 0; i <= idx; i++) {
        ans[i] = str[idx];
        dfs(idx+1);
        ans[i] = ans[i+1];
    }
}
int main() {
    int flag = 1;
    while(gets(str)) {
        if(!flag)
            puts("");
        flag = 0;
        len = strlen(str);
        dfs(0);
    }
    return 0;
}

台長: Morris
人氣(2,853) | 回應(14)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 12406 - Help Dexter
此分類上一篇:[UVA] 12439 - February 29

seo
It's which means that amazing together with inspiring. We really enjoy any designs together with whoever should get it all during the post shall be grinning. thc gummies bulk http://www.riponpress.com/10-best-thc-gummies-to-elevate-your-experience-in-2024/article_70449c56-75ec-11ef-964d-5318f401c14e.html
2025-05-27 23:00:29
sdasds
The website is looking bit flashy and it catches the visitors eyes. Design is pretty simple and a good user friendly interface. delta 9 live resin gummies http://www.essexreporter.com/best-delta-9-gummies-for-a-legal-euphoric-high/article_44bb3ad6-a76f-11ef-8bc0-8b5e27129115.html
2025-06-15 17:54:31
seo
I am just genuinely thrilled to come across this great site along with does get pleasure from looking at valuable content put up below. Your concepts in the publisher ended up being wonderful, cheers to the talk about. wedding underwear for bride http://www.amazon.com/NE-Beauty-Chamber-Magical-Womens/dp/B0DTJFK4SL/
2025-06-24 21:40:39
seo
The a truly great place That i experienced owing to publish it all. The chances of what precisely I want to observe pray during near future you can expect to keep going meant for posting an extremely good place. halo disposable http://meridian-puff.com/product-category/halo-disposable/
2025-06-25 20:29:09
seo
This can be consequently lovely along with inventive. I merely enjoy your hues along with anyone becomes the idea inside snail mail are going to be cheerful. IPTV UK http://xtremehd-uk.uk/
2025-07-09 18:17:38
seo
It all is impressive to read the paper these types of interesting together with different article content for your web-sites. Door Knobs http://www.doorhardware.co.uk/
2025-07-16 19:50:44
seo
This particular is a superb publish We observed because of reveal this. It really is exactly what I needed to determine wish within long term you'll carry on with regard to discussing this type of superb publish. physiotherapy for back pain http://www.blvdwellness.ca
2025-07-17 20:21:16
seo
I just astounded aided by the test most people made to choose this specified share wonderful. Marvelous recreation! index testing http://indextesting32.blogspot.com/
2025-07-30 17:32:02
seo
This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post. https://smtijogja.sch.id/assets/articles/?1xbet_kode_promo_indonesia___bonus_selamat_datang.html http://smtijogja.sch.id/assets/articles/?1xbet_kode_promo_indonesia___bonus_selamat_datang.html
2025-07-31 18:05:27
seo
So i am in fact content with see our site not to mention could have fun with perusing effective content pieces circulated in this case. Typically the creative ideas of this contributor was basically fantastic, thanks a lot for ones show. HD IPTV packages http://xtremehd.co
2025-08-03 17:49:36
JimmySEO
her in fact awesome blog page. her realy content rich and then a this fantastic profession. i prefer this unique. Grok says https://grok.com/share/bGVnYWN5_55e55e94-dd22-47cb-bd31-604a11456ed1
2025-08-03 18:43:44
JimmySEO
her in fact awesome blog page. her realy content rich and then a this fantastic profession. i prefer this unique. Grok says http://grok.com/share/bGVnYWN5_55e55e94-dd22-47cb-bd31-604a11456ed1
2025-08-03 18:46:31
asdas
This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post. 1xbet promo code free bet https://trofeosromero.com/img/pgs/1xbet-promo-code_bonus-code.html
2025-08-03 19:15:40
sdffdsdf
Thanks for sharing us.obtenir un code promo 1xbet http://sukkaphap-d.com/art/code-promo-1xbet___bonus.html
2025-08-03 19:24:27
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文