• 2014年第五届蓝桥杯C/C++ A组国赛 —— 第四题:排列序数


    标题:排列序数

    如果用a b c d这4个字母组成一个串,有4!=24种,如果把它们排个序,每个串都对应一个序号:
    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个两两不同的小写字母,给出它们组成的串,你能求出该串在所有排列中的序号吗?

    【输入格式】
    一行,一个串。

    【输出格式】
    一行,一个整数,表示该串在其字母所有排列生成的串中的序号。注意:最小的序号是0。

    例如:
    输入:
    bdca

    程序应该输出:
    11

    再例如:
    输入:
    cedab

    程序应该输出:
    70

    资源约定:
    峰值内存消耗 < 256M
    CPU消耗 < 1000ms

    请严格按要求输出,不要画蛇添足地打印类似:“请您输入…” 的多余内容。

    所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。

    注意: main函数需要返回0
    注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
    注意: 所有依赖的函数必须明确地在源文件中 #include , 不能通过工程设置而省略常用头文件。

    提交时,注意选择所期望的编译器类型。

    Code

    /*
                                    ^....0
                                   ^ .1 ^1^
                                   ..     01
                                  1.^     1.0
                                 ^ 1  ^    ^0.1
                                 1 ^        ^..^
                                 0.           ^ 0^
                                 .0            1 .^
                                 .1             ^0 .........001^
                                 .1               1. .111100....01^
                                 00                 11^        ^1. .1^
                                 1.^                              ^0  0^
                                   .^                                 ^0..1
                                   .1                                   1..^
                                 1 .0                                     ^  ^
                                  00.                                     ^^0.^
                                  ^ 0                                     ^^110.^
                              0   0 ^                                     ^^^10.01
                       ^^     10  1 1                                      ^^^1110.1
                       01     10  1.1                                      ^^^1111110
                       010    01  ^^                                        ^^^1111^1.^           ^^^
                       10  10^ 0^ 1                                            ^^111^^^0.1^       1....^
                        11     0                                               ^^11^^^ 0..  ....1^   ^ ^
                        1.     0^                                               ^11^^^ ^ 1 111^     ^ 0.
                       10   00 11                                               ^^^^^   1 0           1.
                       0^  ^0  ^0                                                ^^^^    0            0.
                       0^  1.0  .^                                               ^^^^    1 1          .0
                       ^.^  ^^  0^                             ^1                ^^^^     0.         ^.1
                       1 ^      11                             1.                ^^^     ^ ^        ..^
                      ^..^      ^1                             ^.^               ^^^       .0       ^.0
                      0..^      ^0                              01               ^^^       ..      0..^
                     1 ..        .1                             ^.^              ^^^       1 ^  ^0001
                    ^  1.        00                              0.             ^^^        ^.0 ^.1
                    . 0^.        ^.^                             ^.^            ^^^         ..0.0
                   1 .^^.         .^                  1001        ^^            ^^^         . 1^
                   . ^ ^.         11                0.    1         ^           ^^          0.
                    0  ^.          0              ^0       1                   ^^^          0.
                  0.^  1.          0^             0       .1                   ^^^          ..
                  .1   1.          00            .        .1                  ^^^           ..
                 1      1.         ^.           0         .^                  ^^            ..
                 0.     1.          .^          .         0                                  .
                 .1     1.          01          .        .                                 ^ 0
                ^.^     00          ^0          1.       ^                                 1 1
                .0      00           .            ^^^^^^                                   .
                .^      00           01                                                    ..
               1.       00           10                                                   1 ^
              ^.1       00           ^.                                            ^^^    .1
              ..        00            .1                                        1..01    ..
             1.1         00           1.                                       ..^      10
            ^ 1^         00           ^.1                                      0 1      1
            .1           00            00                                       ^  1   ^
             .           00            ^.^                                        10^  ^^
           1.1           00             00                                              10^
           ..^           1.             ^.                                               1.
          0 1            ^.              00                 00                            .^
            ^            ^.              ^ 1                00   ^0000^     ^               01
         1 0             ^.               00.0^              ^00000   1.00.1              11
         . 1              0               1^^0.01                      ^^^                01
          .^              ^                1   1^^                                       ^.^
        1 1                                                                              0.
        ..                                                                              1 ^
         1                                                                               1
       ^ ^                                                                             .0
       1                                                                             ^ 1
       ..                                                          1.1            ^0.0
      ^ 0                                                           1..01^^100000..0^
      1 1                                                            ^ 1 ^^1111^ ^^
      0 ^                                                             ^ 1      1000^
      .1                                                               ^.^     .   00
      ..                                                                1.1    0.   0
      1.                                                                  .    1.   .^
      1.                                                                 1    1.   ^0
     ^ .                                                                 ^.1 00    01
     ^.0                                                                  001.     .^
     */
    
    /* Procedural objectives:
    
     Variables required by the program:
    
     Procedural thinking:
    
     Functions required by the program:
    
     Determination algorithm:
    
     Determining data structure:
    
    
    */
    /* My dear Max said:
    "I like you,
    So the first bunch of sunshine I saw in the morning is you,
    The first gentle breeze that passed through my ear is you,
    The first star I see is also you.
    The world I see is all your shadow."
    
    FIGHTING FOR OUR FUTURE!!!
    */
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int main(){
    	int ans=0;
    	string ss,str;
    	cin>>str;
    	ss=str;
    	sort(ss.begin(),ss.end());
    //	cout<<str<<endl<<ss<<endl;
    	do{
    		if(ss==str)
    			break;
    		ans++;
    //		cout<<ss<<endl;
    	}while(next_permutation(ss.begin(),ss.end()));
    	cout<<ans<<endl;
    	return 0;
    }
    
  • 相关阅读:
    【转】Git: There is no tracking information for the current branch.
    【转】git多个远程仓库
    【转】python批量快速合并excel文件
    【转】HTML5-postMessage实现跨域
    Python3正则表达式search和findall差异讨论
    Python Segmentation fault错误定位办法
    Python3压缩和解压缩实现
    Python3+profile性能分析
    案例:ADG环境遇到redo日志member路径有误以及RMAN-6571错误
    CentOS7的udev的绑定规则
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338253.html
Copyright © 2020-2023  润新知