POJ 1850:Code 组合数学

Code
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 8710   Accepted: 4141

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

题意是给了一个字符串,问其在给定字典中的位置,字符串本身要判断是否符合字典排序。

真的很讨厌做这种题目,感觉没什么分析,不会的话就怎么想都是不会了。但这个题目还算比较简单,找规律,要利用c[i][j]=c[i-1][j-1]+c[i-1][j];预处理出来比这个字符串长度少的所有数量,再从起始字符开始逐个找之前对应长度的字符串,数量都加起来就是答案。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std;

int c[33][33],len,i,sum;
char temp[50];

void init()
{
	int i,j;
	memset(c,0,sizeof(c));

	for(i=0;i<=32;i++)
	{
		for(j=0;j<=i;j++)
		{
			if(j==0||j==i)
				c[i][j]=1;
			else
				c[i][j]=c[i-1][j-1]+c[i-1][j];
		}
	}
}

bool pend(string temp)
{
	for(i=1;i<len;i++)
	{
		if(temp[i]<temp[i-1])
			return false;
	}
	return true;
}

void dfs(int step)
{
	if(step==len)
		return;

}

int main()
{
	while(scanf("%s",temp)!=EOF)
	{
		init();
		sum=0;
		len=strlen(temp);

		if(pend(temp))
		{
			for(i=1;i<len;i++)
			{
				sum+=c[26][i];
			}
			for(i=0;i<len;i++)
			{
				char temp_c;
				if(i!=len-1)
				{
					if(i==0)
						temp_c='a';
					else
						temp_c=temp[i-1]+1;
					for(;temp_c<temp[i];temp_c++)
					{
						sum += c[26-(temp_c-'a'+1)][len-(i+1)];
					}
				}
				else
				{
					if(len==1)
						sum+=temp[i]-'a'+1;
					else
						sum += temp[i]-temp[i-1];
				}
			}
			cout<<sum<<endl;
		}
		else
		{
			cout<<0<<endl;
		}
	}
	return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/lightspeedsmallson/p/4785782.html