WOJ1106-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


#include<stdio.h>
#include<string.h>
int n,le[30],r[10],nr,a[10],na;
char str[30];
int plus(int num,int l){
	for(int i=1;i<=l;i++)
		a[i]=a[i]*num;
	for(int i=1;i<=l;i++){
		if(a[i]>=10000){
			a[i+1]+=a[i]/10000;
			a[i]=a[i]%10000;
		}
	}
	if(a[l+1]!=0)
	l++;
	return l;
}
int add(int la,int lb){
	int l=la>lb?la:lb;
	for(int i=1;i<=l;i++){
		r[i]=r[i]+a[i];
		if(r[i]>=10000){
			r[i+1]+=r[i]/10000;
			r[i]=r[i]%10000;
		}
	}
	if(r[l+1]!=0)
	l++;
	return l;
}
void print(int l){
	for(int i=l;i>=1;i--){
		if(i==l)
		printf("%d",r[i]);
		else
		printf("%04d",r[i]);
	}
	printf("\n");
}
int main(){
	int i,j,tar;
	long long sum,res;
	while((scanf("%d",&n)!=EOF)&&(n!=0)){
		scanf("%s",&str);
		res=0;
		for(i=0;i<10;i++)
			r[i]=0;
		nr=1;
		for(i=0;i<26;i++)
			le[i]=1;
		for(i=0;i<n;i++){
			if(i==0){
				tar=str[i]-'A'+1;
				le[tar-1]=0;
			}
			else{
				sum=0;
				tar=str[i]-'A'+1;
				for(j=0;j<tar;j++)
					sum=sum+le[j];
				le[tar-1]=0;
				tar=sum;
			}
			if(n<=14){
				sum=tar-1;
				for(j=i+1;j<n;j++)
					sum=sum*(26-j);
				res=res+sum;
			}
			else{
				for(j=0;j<10;j++)
					a[j]=0;
				a[1]=tar-1;
				na=1;
				for(j=i+1;j<n;j++)
					na=plus(26-j,na);		
				nr=add(na,nr);
			}
		}
		if(n<=14)
			printf("%lld\n",res);
		else
			print(nr);
	}
	return 0;
} 
//ABCDEFGHIJKLMNOPQRSTUVWXYZ
//ZYXWVUTSRQPONMLKJIHGFEDCBA


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值