Bài toán liệt kê các hoán vị của một tập có lặp theo thứ tự từ điển C/C++

Bài toán liệt kê các hoán vị của một tập có lặp theo thứ tự từ điển C/C++

Bài toán: Cho tập X gồm n chữ cái. X = {a,b,c,…} trong đó các chữ cái có thể lặp lại.

Xem qua lý thuyết

>> Giải thuật liệt kê hoán vị - liệt kê hoán vị tiếp theo theo thứ tự từ điển

Hãy liệt kê các hoán vị của tập X trên theo thứ tự từ điển.

[Input]

Có thể có nhiều hơn một test case trong file dữ liệu đầu vào PERMUTATION.INP. Dòng đầu tiên ghi số test case T.

Theo sau là danh sách test case.

Các dòng tiếp theo là string đầu vào.

[OutPut]

Ghi ra file PERMUTATION.OUT liệt kê tất cả các hoán vị theo thứ tự từ điển.

[I/O Example]

Input

2
bbjd
abcd

Output

bbdj ← in the order of b, b, d, and j
bbjd
bdbj
bdjb
bjbd
bjdb ← in the order of b, j, d, and b
dbbj
dbjb
djbb
jbbd
jbdb
jdbb

 

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

Chương trình sinh hoán vị kế tiếp của tập n phần tử có lặp.

#include <cstdio>

#include <iostream>

#include<string.h>

using namespace std;

char str[11];

int n;

void toPermutation(){

          while(true){

                    //print out.

                    for(int index = 0;index <n; index++){

                              cout<<str[index];

                    }

                    cout<<"\n";

                    // find j that arr[j] < arr[j+1]

                    int j = n -2;

                    while(j>=0 && str[j] >= str[j+1]){

                              j--;

                    }

                    if(j == -1) break;

                    // find index on the right that is minimum and greater than arr[j].

                    int minRightIndex = j+1;

                    for(int k=j+2;k<n;k++){

                              if(str[k] <= str[minRightIndex] && str[k] > str[j]){

                                        minRightIndex = k;

                              }

                    }

                    // wrap j and minRightIndex

                    char temp = str[j];

                    str[j] = str[minRightIndex];

                    str[minRightIndex] = temp;

                    //revert arr from j+1 to n

                    for(int loop = 0; loop < (n -1 -j)/2;loop++){

                              char temp = str[j +1 + loop];

                              str[j+1+loop] = str[n- 1 -loop];

                              str[n -1 -loop] = temp;

                    }

                    

          }

}

int main(int argc, char** argv)

{

          int tc, T;

          freopen("PERMUTATION.INP", "r", stdin);

          freopen("PERMUTATION.OUT", "w", stdout);

          cin >> T;

          for(tc = 0; tc < T; tc++)

          {

                    cin>>str;

                    n = strlen(str);

                    for(int i=0;i<n-1;i++){

                              for(int j=i+1;j<n;j++){

                                        if(str[i] > str[j]){

                                                  char temp = str[i];

                                                  str[i] = str[j];

                                                  str[j] = temp;

                                        }

                              }

                    }

                    toPermutation();

        cout<<"\n";

                    // Print the answer to standard output(screen).

                    

          }

          return 0;//Your program should return 0 on normal termination.
 

}
Bạn thấy bài viết này như thế nào?: 
Average: 10 (2 votes)
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
State of Drupal presentation (June 2014)

Dries Buytaert phát biểu về State của Drupal presentation tại DrupalCon Austin

I gave my traditional State of Drupal presentation this week at DrupalCon Austin.

Chrome giật ngôi vương của Internet Explorer chỉ trong 1 ngày

Chrome giật ngôi vương của Internet Explorer chỉ trong 1 ngày

Chrome bất ngờ vượt mặt Internet Explorer để trở thành trình duyệt phổ biến nhất thế giới trong ngày 18/3, tuy nhiên ngay sau đó IE lại trở về vị trí thứ nhất quen thuộc của mình.

Number 2: Security 5 lỗi cần tránh trong Drupal Website

Number 2: Security 5 lỗi cần tránh trong Drupal Website

Good security practices protect your site from hacker attacks. In this article we'll look at some methods for reducing security risks on your site. 

Công ty diệt chuột T&C

 

Diet con trung