首页 > 专题系列 > Java解POJ > POJ 1850 Code [解题报告] Java
2013
11-10

POJ 1850 Code [解题报告] Java

Code

问题描述 :

Transmitting and memorizing information is a task that requires different coding systems for the best use of the available space. A well known system is that one where a number is associated to a character sequence. It is considered that the words are made only of small characters of the English alphabet a,b,c, …, z (26 characters). From all these words we consider only those whose letters are in lexigraphical order (each character is smaller than the next character).

The coding system works like this:

• The words are arranged in the increasing order of their length.

• The words with the same length are arranged in lexicographical order (the order from the dictionary).

• We codify these words by their numbering, starting with a, as follows:

a - 1

b - 2



z - 26

ab - 27



az - 51

bc - 52



vwxyz - 83681

Specify for a given word if it can be codified according to this coding system. For the affirmative case specify its code.

输入:

The only line contains a word. There are some constraints:

• The word is maximum 10 letters length

• The English alphabet has 26 characters.

输出:

The output will contain the code of the given word, or 0 if the word can not be codified.

样例输入:

bf

样例输出:

55

解题代码:

//* @author: [email protected]
import java.util.*;
public class Main
{
 public static void main(String[] args)
 {
  Scanner in=new Scanner(System.in);
  String s=in.next();
  boolean bb=false;
  for(int i=0;i< s.length()-1;i++)
  {
	if(s.charAt(i)-s.charAt(i+1)>=0)
	{
		System.out.println(0);
		bb=true;
		break;
	}	
    }
   if(bb) System.exit(0);
   int l=s.length();
   long ans=0;
   for(int i=1;i< l;i++)
	ans+=find(26,i);
   int k=0;
   for(int i=0;i< l;i++)
   {
	k=0;
	if(i!=0) k=s.charAt(i-1)-'a'+1;
	int y=s.charAt(i)-'a'+1;
	for(int j=y-1;j>k;j--)
	{
		ans+=find(26-j,l-i-1);
	}
    }
   System.out.println(ans+1);
 }

 public static long find(int u,long a)
 {
	if(u< a) return 0;
	if(a==0) return 1;
	long l=1;
	for(int i=u;i>u-a;i--)
		l*=i;
	for(int i=1;i<=a;i++)
		l/=i;
	return l;
  }
}