WOJ1106-String's Puzzle

来源:互联网 发布:苹果电脑图片设计软件 编辑:程序博客网 时间:2024/05/18 21:43

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.

样例输入

3ABC3ZYX 3ABD4ABCD0

样例输出

01559910


#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]);elseprintf("%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);elseprint(nr);}return 0;} //ABCDEFGHIJKLMNOPQRSTUVWXYZ//ZYXWVUTSRQPONMLKJIHGFEDCBA


原创粉丝点击