Ordinal number of the fifth edition of the Lanqiao Cup

Time limit: 1Sec Memory limit: 128MB
Title description
If you use the 4 letters of abcd to form a string, there are 4!=24 types. If you put them in order, each string corresponds to a serial number:
abcd 0
abdc 1
acbd 2
acdb 3
adbc 4 adcb
5
bacd 6
badc 7
bcad 8
bcda 9
bdac 10
bdca 11
cabd 12
cadb 13
cbad 14
cbda 15
cdab 16
cdba 17

现在有不多于10个两两不同的小写字母,给出它们组成的串,你能求出该串在所有排列中的序号吗?

Enter
one line, one string.
Output
one line, an integer, which represents the serial number of the string in the string generated by all permutations of its letters. Note: The smallest serial number is 0.
Sample input
bdca
sample output
11

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
    
    
    struct pailie{
    
    
        char array[5];
        int n;
    };
    struct pailie str[30];
     char sz[5]={
    
    "abcd"};
	 str[0].n=0;
	strcpy(str[0].array,sz);
    for(int i=1;i<=23;i++)
    {
    
    
    	next_permutation(sz,sz+4);
        strcpy(str[i].array,sz);
        //puts(str[i].array);
        str[i].n=i;
	}
    char  array[5];
    gets(array);
    for(int i=0;i<24;i++)
    {
    
    
        int xb=1;
        xb=strcmp(array,str[i].array);
        if(xb==0)
        {
    
    
            cout<<str[i].n<<endl;
            break;
        }
        
    }
   return 0;
}

Guess you like

Origin blog.csdn.net/qq_46232829/article/details/108313685