首页 > 专题系列 > Java解POJ > POJ 3226 String’s Puzzle [解题报告] Java
2013
11-12

POJ 3226 String’s Puzzle [解题报告] Java

String’s Puzzle

问题描述 :

Zhu Ming and Small Li find a strange stone tablet, which has some capitals printed on it. Capitals in every string are different with each other, and every capital string is followed by a number. Because of their supernormal intelligence, Zhu Ming and Small Li find the number following the string is numbered in the order of dictionary sequence from little to big (the original number is 0).For example, when the length of capital string is 3, number following the string will be:
 
ABC 0
ABD 1
……  
ZYX 15599

To better study this puzzle, please make a program to given a n-length string (1<=n<=26), output the number of the string.

输入:

There are several test cases.
Each data is made up of two lines.
The first line is number n (1<=n<=26)
The second line is a string with its length of n.
n=0 means end of the input.

输出:

Output the string number for each group of data tested.

样例输入:

3
ABC
3
ZYX 
3
ABD
4
ABCD
0

样例输出:

0
15599
1
0

解题代码:

//* @author:alpc12
import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
	
BigInteger[][] Ank = new BigInteger[27][27];
	
void pre() {
 BigInteger i, j;
 for(i = BigInteger.ONE; i.compareTo(new BigInteger("27")) != 0; i = i.add(BigInteger.ONE)) {
  Ank[i.intValue()][0] = BigInteger.ONE;
  Ank[i.intValue()][1] = i;
  for(j = new BigInteger("2"); j.compareTo(i) <= 0; j = j.add(BigInteger.ONE)) {
   Ank[i.intValue()][j.intValue()] = Ank[i.intValue()][j.intValue()-1].multiply(i.subtract(j).add(BigInteger.ONE));
  }	
 }
}
	
 BigInteger cal(String s, int n, int pos) {
   BigInteger ret = BigInteger.ZERO;
   boolean[] chk = new boolean[26];
   int i, ava = s.charAt(pos)-'A';
   for(i = 0; i < pos; ++i)
	chk[s.charAt(i)-'A'] = true;
   for(i = 0; i < s.charAt(pos)-'A'; ++i)
	if(chk[i])
   ava--;
   BigInteger a = new BigInteger(""+ava);	
   if(pos == n-1)
	return a;
   ret = ret.add(a.multiply(Ank[26-pos-1][n-pos-1]));
   return ret.add( cal(s, n, pos+1) );
  }
	
  void run() {
   Scanner cin = new Scanner(System.in);
   pre();
   while(true) {
	int n = cin.nextInt();
	if(n == 0) 
		break;
	String s = cin.next();
	System.out.println(cal(s, s.length(), 0));
   }
  }

 public static void main(String[] args) {
	new Main().run();

 }

}

  1. 第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。