组合数学poj 1496 1850 同样的代码过两题
Description 1942
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 word is maximum 10 letters length
• The English alphabet has 26 characters.
Output
Sample Input
bf
Sample Output
55 1496
Description
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 will be terminated by end-of-file.
Output
Sample Input
z
a
cat
vwxyz
Sample Output
26
1
0
83681
#include"iostream"
#include"cstdio"
#include"cstring"
using namespace std;
const int ms=;
int c[ms+][ms+];
int main()
{
int i,j;
//bool flag;
for(i=;i<=ms;i++)
{
c[i][]=c[i][i]=;
}
for(i=;i<=ms;i++)
for(j=;j<i;j++)
c[i][j]=c[i-][j]+c[i-][j-];
char str[ms];
while(scanf("%s",str)!=EOF)
{
bool flag=false;
int len=strlen(str);
for(i=;i<len;i++)
if(str[i]<=str[i-])
{
flag=true;
break;
}
if(flag)
{
printf("0\n");
continue;
}
int ans=;
i=;
while(i<len)
ans+=c[][i++];
i=-;
int k=;
while(k<len)
{
i++;
if(k==len-)
{
//if(i+'a'<=str[k])
while(i+'a'<=str[k])
{
ans++;
i++;
}
break;
}
while(i+'a'<str[k])
{
//if(i+'a'<str[k])
//{
ans+=c[-i-][len--k];
i++;
continue; }
k++;
}
printf("%d\n",ans);
}
return ;
}
最新文章
- Python 操作 MS Excel 文件
- for循环
- javascript基础语法——变量和标识符
- C#正则表达式教程和示例
- Lock的实现之ReentrantLock详解
- java使用Cipher进行签名和验签
- Winform跨线程操作界面的策略
- 使用inotify检测linux目录内文件变化
- Contest1065 - 第四届“图灵杯”NEUQ-ACM程序设计竞赛(个人赛)C粉丝与汉诺塔
- 解决pxe网络批量安装部署linux遇到的问题和解决方法
- 清橙OJ 1082 查找第K小元素 -- 快速排序
- msql 按值排序
- TTimerThread和TThreadedTimer(都是通过WaitForSingleObject和CreateEvent来实现的)
- js数组总结
- Linux下C/C++程序调试基础(GCC,G++,GDB,CGDB,DDD)
- javaScript(5)---运算符
- linux 启动springboot项目
- cookie 就是一些字符串信息
- javascript对象与方法
- 计算机信息类ComputerInfo(车)
热门文章
- [Hive - LanguageManual] Create/Drop/Alter -View、 Index 、 Function
- JVM内存结构
- Git 一些日常使用积累
- HDU 5794 A Simple Chess (容斥+DP+Lucas)
- 新浪云sae 邮件服务 quicksend()
- POJ 3026 Borg Maze(bfs+最小生成树)
- hdu 2824 The Euler function
- 转载github上最全的资源教程--前端涉及的所有知识体系
- POJ 1679 The Unique MST (次小生成树)
- MFC实现数独(2)