Enumeration of oj 2030 permutation numbers of Fuzhou University

Source: Internet
Author: User
/* ACMer: The MDK time is the slowest one... Khan. UserID: MDKSubmit time: 17: 29: 16 Language: C ++ Length: 2061 Bytes. result: Accepted */# include <stdio. h> # include <iostream> # include <limits. h> # include <string. h> # include <math. h> # include <vector> # include <algorithm> # include <set> # include <map> # include <stack> # define MAXN 2000 using namespace std; char c [20], a [20]; string ku = "0000000000000000000"; void fun (int n, int r) {if (n) {fun (n/r, r); ku + = n % r> 9? N % R-10 + 'A': n % r + '0';} int next (int j) {for (int I = j + 1; I <strlen (a); I ++) if (a [I]! = '') {// Cout <" next (j): "<I <endl; return I;} return MAXN;} int OK () {for (int I = 0; I <strlen (a); I ++) {for (int j = 0; j <strlen (a); j ++) {if (a [j] = '(' & next (j )! = MAXN & a [next (j)] = ') {a [j] = ''; a [next (j)] = '';}}} for (int I = 0; I <strlen (a); I ++) if (a [I]! = '') Return 0; return 1;} int main () {while (cin> c) {int n = 0, d [20], flag = 0, num = 0; for (int I = 0; I <strlen (c); I ++) {if (c [I] = '? ') D [n] = I, n ++;} if (n = 0) cout <"-0" <endl; else {for (int I = 0; I <pow (2, n); I ++) {fun (flag ++, 2); strcpy (a, c ); for (int j = 0; j <n; j ++) {if (ku [ku. length ()-1-j] = '0') a [d [j] = '('; if (ku [ku. length ()-1-j] = '1') a [d [j] = ')';} if (OK () {num ++ ;} ku = "000000000000000000" ;}cout <num <endl ;}}}

The main idea is enumeration, which is a very stupid method. The Ugly code is stuck.

I found a method to enumerate all the sorting conditions. The main principle is to use the hexadecimal limit.

String ku = "0000000000000000000 ";
Void fun (int n, int r) {// converts every hexadecimal value in the 10 hexadecimal format. Thanks for the simplified code.
If (n ){
Fun (n/r, r );
Ku + = n % r> 9? N % R-10 + 'A': n % r + '0 ';
}
}

Here we use the binary number, because there are only two enumeration cases: (and). There are several hexadecimal values.

Then hash:

1                 fun(flag++,2);
2 strcpy(a,c);
3 for(int j = 0;j<n;j++)
4 {
5 if(ku[ku.length()-1-j]=='0')
6 a[d[j]]='(';
7 if(ku[ku.length()-1-j]=='1')
8 a[d[j]]=')';
9 }

The next step is to determine whether the function meets the conditions, which is easy.

Enumeration of permutation numbers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.