zoukankan      html  css  js  c++  java
  • 字母组合

    字母A,B,C的所有可能的组合(按字典顺序排序)是:A, AB,ABC,AC,B,BC,C 每个组合都对应一个字典顺序的序号,如下所示:

                1 A

                2 AB

                3 ABC

                4 AC

                5 B

                6 BC

                7 C

          找出某个字母组合的字典序号。例如,上例中AC的字典序号是4。

          注:假设某个字母组合为X1X2X3…XK,保证X1<…

          输入:输入包括2行:

          第一行:N,表示字母组合由字母表中前N(N<=26)个字母组成;

          第二行:某一个字母组合,都是大写字母;

          输出:该字母组合的序号;

          输入样例:

      3

           AB

          输出样例: 2

    分析:比较简单的一道题,枚举这个字符串的每一位,看看比这一位小的能组成的字符串有多少个.比如一个字符串CD,当前枚举到了第一位,那么就统计第一位放A/B能够成多少个字符串,统计完后再来枚举下一位,以此类推,方法和poj1850差不多.

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int n, len, c[30][30], num[30], ans;
    char s[30];
    
    int main()
    {
        scanf("%d", &n);
        scanf("%s", s + 1);
        len = strlen(s + 1);
        c[0][0] = 1;
        for (int i = 1; i < 30; i++)
        {
            c[i][0] = 1;
            for (int j = 1; j < 30; j++)
                c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
        }
        for (int i = 1; i <= len; i++)
            num[i] = s[i] - 'A' + 1;
        for (int i = 1; i <= len; i++)
        {
            int ch;
            if (i == 1)
                ch = 1;
            else
                ch = num[i - 1] + 1;
            while (ch < num[i])
            {
                for (int j = i + 1; j <= n; j++)
                ans += c[n - ch][j - i];
                ans++;
                ch++;
            }
            if (ch == num[i] && i != len)
            ans++;
        }
        ans++;
        printf("%d
    ", ans);
    
        return 0;
    }
  • 相关阅读:
    xcode6创建工程时 默认去掉了PrefixHeader.pch
    KVC访问私有成员
    Apple Watch 中Context Menu的应用
    Apple Watch应用创建
    NSURLConnection加载数据并展示
    UIView 的exclusiveTouch clipsToBounds和transform属性
    Shell的一些基本用法
    NS_ENUM和NS_OPTIONS
    iOS国际化时遇到错误: the data couldn't be read because it isn't in the correct format.
    iOS8中UIAlertController的使用
  • 原文地址:https://www.cnblogs.com/zbtrs/p/7954226.html
Copyright © 2011-2022 走看看