Trang

Thứ Bảy, 1 tháng 1, 2011

1105. Word Index

import java.io.*;
import java.util.*;

public class Main {

    /**
     * http://acm.tju.edu.cn/toj/showp1105.html
     * @author hunglee
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {

        Scanner in = new Scanner(System.in);
        
        String s;
        int res = 0;
        int i, j;
        while (in.hasNext()) {
            s = in.nextLine();
            if (s == "") break;
            
            res = 0;
            for (i = 0; i < s.length() - 1; i++)
                if (s.charAt(i) >= s.charAt(i+1)) {
                    System.out.println(0);
                    s = "";
                    break;
                }
            if (s == "") continue;
            for (i = 1; i < s.length(); i++) {
                res += (c(26, i));
            }
            res++;
            int pre = 'a' - 1;
            for (i = 0; i < s.length(); i++) {
                for (j = pre + 1; j < s.charAt(i); j++)
                    res += c('z' - j, s.length() - i - 1);
                pre = s.charAt(i);
            }
            
            System.out.println(res);
        };
    }
    
    protected static int c(int n, int k) {
        long res, res2;
        res = res2 = 1;
        int i;
        for (i = n - k + 1; i <= n; i++) 
            res *= i;
        for (i = 2; i <= k; i++)
            res2 *= i;
        return (int)(res / res2);
    }

}

Không có nhận xét nào:

Đăng nhận xét