POJ 1496 Word Index 和POJ 1850 Code 动态规划

本文介绍了一种简单的编码方案,用于将特定类型的单词(最多五个字母且按字母顺序排列)转换为整数,并提供了一个高效的算法实现。

POJ 1496
Word Index
Time Limit:1000MS Memory Limit:10000K
Total Submissions:3035 Accepted:1731

Description

Encoding schemes are often used in situations requiring encryption or information storage/transmission economy. Here, we develop a simple encoding scheme that encodes particular types of words with five or fewer (lower case) letters as integers.

Consider the English alphabet {a,b,c,...,z}. Using this alphabet, a set of valid words are to be formed that are in a strict lexicographic order. In this set of valid words, the successive letters of a word are in a strictly ascending order; that is, later letters in a valid word are always after previous letters with respect to their positions in the alphabet list {a,b,c,...,z}. For example,

abc aep gwz

are all valid three-letter words, whereas

aab are cat

are not.

For each valid word associate an integer which gives the position of the word in the alphabetized list of words. That is:

a -> 1
b -> 2
.
.
z -> 26
ab -> 27
ac -> 28
.
.
az -> 51
bc -> 52
.
.
vwxyz -> 83681

Your program is to read a series of input lines. Each input line will have a single word on it, that will be from one to five letters long. For each word read, if the word is invalid give the number 0. If the word read is valid, give the word's position index in the above alphabetical list.

Input

The input consists of a series of single words, one per line. The words are at least one letter long and no more that five letters. Only the lower case alphabetic {a,b,...,z} characters will be used as input. The first letter of a word will appear as the first character on an input line.

The input will be terminated by end-of-file.

Output

The output is a single integer, greater than or equal to zero (0) and less than or equal 83681. The first digit of an output value should be the first character on a line. There is one line of output for each input line.

Sample Input

z
a
cat
vwxyz

Sample Output

26
1
0
83681
解题报告:
刚刚看到这道题首先想到的是直接表示,但是看了一会儿,感觉没有那么简单,头脑感觉可以用动态规划
首先想到用opt[i][j]来表示第i位是j的对应的权值,想了一会儿,状态方程没有找出来,高手可以给指点一下,小弟不胜感激
然后看到网上的解题报告
用opt[i][j]表示长度为i以j开头的串总数
sum[i]表示长度为i的串的总数
状态转移方程为opt[i][j]=opt[i][j+1]+opt[i-1][j+1]
例如a__表示长度为3,a为开头的串,这样的串有,a后面跟bc,bd,...........bz,(数目正好是opt[i-1][j+1])
然后就是cd,ce,.........de.........yz(数目正好是opt[i][j+1],因为把a换成b,就成了bcd,bce,......,bxy)
/* Author yan*/
#include<stdio.h>

int opt[6][28];
int sum[6];

int valid(char *ch)
{
	int _i;
	for(_i=0;ch[_i+1]!='/0';_i++)
	{
		if(ch[_i]>=ch[_i+1]) return 0;
	}
	return 1;
}

int main()
{
	int i,j;
	char cache[8];
	int ans=0;
	int s_len;
/*
边界条件初始化
*/
	for(i=1;i<=26;i++)
	{
		opt[1][i]=1;
	}
	sum[2]=1,sum[3]=1,sum[4]=1,sum[5]=1;//由于后面的循环中没有累加像yz,xyz,wxyz.....这样的串,于是直接置1
	opt[2][25]=1,opt[3][24]=1,opt[4][23]=1,opt[5][22]=1;
	sum[1]=26;
	for(i=2;i<=5;i++)
	{
		for(j=26-i;j>0;j--)
		{
			opt[i][j]=opt[i][j+1]+opt[i-1][j+1];
			sum[i]+=opt[i][j];
		}
	}
	//freopen("input","r",stdin);
	while(scanf("%s",cache)!=EOF)
	{
		ans=0;
		if(valid(cache)==0)
		{
			printf("0/n");
		}
		else
		{
/*
我们以串dgk来说明问题
*/
			s_len=strlen(cache);
			for(i=1;i<s_len;i++)	ans+=sum[i];//先累加长度为1和长度为2的串数
			for(i=1;i<=cache[0]-'a';i++) ans+=opt[s_len][i];//累加以a开头长度为3的串到以c开头长度为三的串的个数
/*
循环的核心部分
下面的循环实际上实现的是下面的累加情况
i=2时,opt[2][e]+......+opt[2][f]
i=1是,opt[1][h]+......+opt[1][j]
由于opt[1][k]没有计入
最后ans+1
*/
			for(i=s_len-1;i>0;i--)
				for(j=cache[s_len-i-1]-'a'+2;j<cache[s_len-i]-'a'+1;j++) ans+=opt[i][j];
			printf("%d/n",ans+1);
		}
	}
	return 0;
}
POJ  1850一样,代码如下
/* Author yan*/
#include<stdio.h>
int opt[13][28];
int sum[13];

int valid(char *ch)
{
	int _i;
	for(_i=0;ch[_i+1]!='/0';_i++)
	{
		if(ch[_i]>=ch[_i+1]) return 0;
	}
	return 1;
}

int main()
{
	int i,j;
	char cache[13];
	int ans=0;
	int s_len;
	for(i=1;i<=26;i++)
	{
		opt[1][i]=1;
	}
	for(i=2;i<=10;i++)
	{
		sum[i]=1;
		opt[i][26-i+1]=1;
	}
	sum[1]=26;
	for(i=2;i<=10;i++)
	{
		for(j=26-i;j>0;j--)
		{
			opt[i][j]=opt[i][j+1]+opt[i-1][j+1];
			sum[i]+=opt[i][j];
		}
	}
	//freopen("input","r",stdin);
	scanf("%s",cache);
	ans=0;
	if(valid(cache)==0)
	{
		printf("0/n");
	}
	else
	{
		s_len=strlen(cache);
		for(i=1;i<s_len;i++)	ans+=sum[i];
		for(i=1;i<=cache[0]-'a';i++) ans+=opt[s_len][i];
		for(i=s_len-1;i>0;i--)
			for(j=cache[s_len-i-1]-'a'+2;j<cache[s_len-i]-'a'+1;j++) ans+=opt[i][j];
		printf("%d/n",ans+1);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值