poj1850 Code

14 篇文章 0 订阅
2 篇文章 0 订阅
Code
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 7638 Accepted: 3591

Description

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. 

Input

The only line contains a word. There are some constraints: 
• The word is maximum 10 letters length 
• The English alphabet has 26 characters. 

Output

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

Sample Input

bf

Sample Output

55


大致题意: 给定一个字符串,问这个字符串按照给定规律的位置。

         规律:字符串<=10,(s[i-1] < s[i]),如果存在s[i-1] >= s[i],则输出0

解题思路:找出规律就容易解答了,当有一个字符时, ans = 26;

         当有两个字符时a(b-z) = 25, b(c-z) = 24, c(d-z) = 23, ... ,yz = 1.

                      = c(25,1) + c(24,1) + ... + c(n, 1) + c(n-1,1) + c(1,1)

                    因为  c(n,m) = c(n-1,m-1) +c(n-1,m);

                         c(n-1,m) = c(n-2, m-1) + c(n-2, m);

                         c(n-2,m) = c(n-3, m-1) + c(n-3, m);

                         ...

                         c(m+1, m) = c(m, m-1) + c(m, m) = c(m, 1) + 1;

                         c(m, m) = 1;

                    所以  c(n, m) = c(n-1, m-1) + c(n-2, m-1) + ... + c(m, m-1) + 1;

             c(n, m) =c(n-1, m-1) + c(n-2, m-1) + ... + c(2, m-1) + c(1, m-1);

              (c(s,t) = 0 t>s);

           所以两个字符为c(26,2), 同理 3个 c(26,3) ... 10个 c(26, 10);

例如给bfgi  = 1.[len<4的所有] + 2.[len = 4(小于 bfgi)];

           1. 将所有len<4的c(26,i) 和计算;

           2. 就要枚举<bfgi(长度相等的字符串), 如:a..., (ba.. to be..), (bfg. to bfgh)

           也就是为a时 c('z' - 'a', len-pos(a)-1), 为什么是('z' - 'a'),

           因为a下一个,从b-z,len-pos(a)-1就是除了a余下的长度,同理可得余下的。


代码:

#include <stdio.h>
#include <string.h>

int sum;
int c[27][27] = {0};

char str[20];

void init()
{
	int i, j;
	for (i=0; i<27; ++i)
	{
		for (j=0; j<=26; ++j)
		{
			if (!j || i==j)
				c[i][j] = 1;
			else
				c[i][j] = c[i-1][j-1] + c[i-1][j];

		}
	}

}

void solve()
{
	int len = strlen(str);
	sum = 0;
	int i, j;
	for (i=1; i<len; ++i)
		sum += c[26][i];
	char ch;
	for (i=1; i<len; ++i)
	{
		if (str[i-1] >= str[i])
		{
			puts("0");
			return;
		}
	}
	for (i=0; i<len; ++i)
	{
		ch = i==0?'a':str[i-1]+1;
		while (ch < str[i])
		{
			sum += c['z' - ch][len-i-1];
			ch++;
		}
	}
	sum++;
	printf("%d\n", sum);
}

int main()
{
	init();
	while (~scanf("%s", str))
	{
		solve();
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值