POJ 1256 Anagram (input can be re-set enumeration sort)

Source: Internet
Author: User

"Test Instructions brief": The subject Test instructions very good understanding! The hint of the topic gives us a clearer understanding of the key points.

A upper case letter goes before the corresponding lower case letter.
The right order of letters is ' a ' < ' a ' < ' B ' < ' B ' <...< ' z ' < ' z '.

is to give a sequence (the sequence can have repeating elements), let's output it all arranged, alphabetic order given!

"Analysis": This is the question I found before I learned about enumeration ordering and subset generation, related algorithms for enumeration ordering and subset generation in this book on the introduction to algorithmic competition the thinking learning--enumeration ordering and subset generation!


/*date:2014/11/02by:vidattention: The only difficulty with this problem is enumerating the permutations, of course, the method is not unique, you can use the STL, or you can write your own enumeration functions like mine. Also is in the input at the same time write a letter according to the title of the order of precedence, and then call the enumeration sort function, to get the result! *///232K 329ms#include<iostream> #include <cstring>using namespace std; #define N 14char p[n],a[n];void Order (char* p) {int I,j;int len = strlen (p); char tmp;for (i = 0;i<len-1;i++) {for (j = i+1;j<len;j++) {if ((p[i]>p[j ]) && (p[j]>= ' a ')) | | ((p[i]< ' a ') && (P[i]>p[j]) && (p[j]>= ' a ')) | | ((p[i]>= ' a ') && (p[j]< ' a ') && ((p[i]-' a ') >= (p[j]-' a ')) | | ((p[i]< ' a ') && (p[j]>= ' a ') && ((p[i]-' a ') > (p[j]-' a ')))) {tmp = p[i];p [i] = P[j];p [j] = tmp;}}} for (i =0;i<len;i++)//cout<<p[i]<<endl;} Recursion ... void print_pumutation (int n,char p[],char a[],int cur) {int i,j;if (cur = = N)//cur refers to the current position! {for (i = 0;i<n;i++) Cout<<a[i];cout<<endl;} else for (i = 0;i<n;i++) if (!i| | P[I]!=P[I-1])//This is to check the first element of P and all elements that are not the same as the first element,//This is the only time we are calling it recursively! {int C1 = 0,C2 = 0;for (j= 0;j<cur;j++) if (a[j] = = P[i]) c1++; The number of occurrences of p[i] in a[0]~a[cur-1]. for (j = 0;j<n;j++) if (p[i] = = P[j]) c2++; The number of occurrences of p[i] in the array p. if (C1&LT;C2) {a[cur] = P[i];p rint_pumutation (n,p,a,cur+1);}}} int main () {int test;int len;cin>>test;while (test--) {Cin>>p;len = strlen (P); order (p);p rint_pumutation ( len,p,a,0);} return 0;}




POJ 1256 Anagram (input can be re-set enumeration sort)

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.