字典序问题(Java)

Description

在数据加密和数据压缩中常需要对特殊的字符串进行编码。
给定的字母表A由 26 个小写英文字母组成A={a,b,…,z}。
该字母表产生的升序字符串是指字符串中字母按照从左到右出现的次序与字母在字母表中出现的次序相同,且每个字符最多出现1次。
例如,a,b,ab,bc,xyz等字符串都是升序字符串。现在对字母表A 产生的所有长度不超过6 的升序字符串按照字典序排列并编码如下。
img
对于任意长度不超过6 的升序字符串,迅速计算出它在上述字典中的编码。对于给定的长度不超过6 的升序字符串,计算出它在上述字典中的编码。

Input

输入数据的第一行是一个正整数k,表示接下来共有k行。接下来的k行中,每行给出一个字符串。

Output

将计算结果输出,共有k行,每行对应于一个字符串的编码,对于非法字符串序列输出0。

Sample Input

2
a
b

Sample Output

1
2
import java.io.*;

public class Main {
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

    public static String nextString() throws IOException {
        in.nextToken();
        return in.sval;
    }

    public static int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }

    public static void main(String[] args) throws IOException {
        int n = nextInt();              // 读入n行数据
        char[] str;                     // 每行的字符串
        int str_num[] = new int[10];    // 字符串长度不超过10
        // 按字符串顺序存储每个字符的序列, a存1, b存2

        while (n-- > 0) {
            int sum = 0;    //初始化为0
            str = nextString().toCharArray();   // 输入的String转成char
            boolean flag = false;               // 非法字符串标志为false

            // 从字符串第一个字符开始,挨个与后面相邻的作比较,判断是否为非法字符串
            for (int i = 0; i < str.length - 1; i++)
                if (str[i] >= str[i + 1] || str[i] < 'a' || str[i] > 'z') {
                    flag = true;        //如果不是升序字符串或者有非小写字母的, 非法字符串标记true
                    break;
                }
            // 非法字符串输出0
            if (flag) {
                System.out.println(sum);
                continue;
            }
            // 单个字母a为1, b为2, 后面加的都是除了本身的
            // 比如a b c d, d为4, sum = 3(前三个), 还得补上本身的
            // 比如ab是27, sum + 前面26个字母 = 26, 所以得先加一个1
            sum++;

            // 统计每个字母的编码, a = 1, b = 2 ...... z = 26
            for (int i = 0; i < str.length; i++)
                str_num[i] = str[i] - 'a' + 1;

            // 计算小于当前长度的所有字符串情况
            // 比如字符串dfgh, 计算字符串长度为1, 2, 3的所有情况
            // a-z   ab - yz   abc - xyz   abcd - dfgh
            // 组合公式
            for (int i = 1; i < str.length; i++)
                sum += C(26, i);

            // 从初始字母a开始, temp即为1
            // 统计abcd - dfgh中间的个数
            // abcd - axyz   bcde - bxyz  cdef - cxyz  defg - dfgh
            // d efg - d exy  d fgh
            // dfgh在str_num中存为{4, 6, 7, 8}
            int temp = 1;
            for (int i = str.length; i > 0; i--) {  // 字符串越来越短
                //从temp 计算到 str_num, a算到d(不包含d)
                for (int j = temp; j < str_num[str.length - i]; j++)
                    sum += C(26 - j, i - 1);
                //dfgh第一次循环计算abcd - axyz   bcde - bxyz  cdef - cxyz
                temp = str_num[str.length - i] + 1;     //temp存当前str_num元素 + 1
                // dfgh第一次循环temp = 'd' - 'a' + 1 + 1 = 5;  也就是‘e’
                // d efg - d exy  d fgh
            }
            out.println(sum);
            out.flush();
        }
        out.close();
    }

    //计算C(n,m) = n! / (m! * (n-m)!)
    public static int C(int n, int m) {
        int a, b;
        a = 1;
        b = 1;
        for (int i = n; i > n - m; i--)
            a *= i;
        for (int i = 1; i <= m; i++)
            b *= i;
        return a / b;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值