poj1496 Word Index / poj1850 Code(组合数学)

poj1850 Code

题意:输出若干个给定的字符串($length<=10$)在字典序中的位置,字符串中的字母必须严格递增。

读取到非法字符串时,输出“0”,终止程序。(poj1496:继续读取)

我们分成2种情况讨论字典序小于给定字符串的字符串个数

1.长度比给定字符串小

其实长度为$i$的字符串的个数就是$C(26,i)$

因为我们随机选取$i$个字符后,从小到大依次取出,是可以保证字符串是有序的。

2.长度等于给定字符串

我们枚举每一位,计算从该位开始小于给定字符串的个数,同样可以用组合数计算。

预处理组合数一下........

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #define re register
 5 using namespace std;
 6 char a[20];
 7 int C[30][30];
 8 int main(){
 9     for(int i=1;i<=26;++i)
10         for(int j=0;j<=i;++j)//组合数预处理
11             C[i][j]=(!j||i==j)?1:C[i-1][j]+C[i-1][j-1];
12     while(cin>>a){//用scanf就OLE(大雾)
13         int len=strlen(a),ans=1; bool flag=0;
14         for(int i=1;i<len;++i)
15             if(a[i]<=a[i-1]){
16                 printf("0\n");
17                 flag=1; break;
18             }
19         if(flag) continue;//1850改为return 0;
20         for(int i=1;i<len;++i) ans+=C[26][i];//长度<len
21         for(int i=0;i<len;++i){//长度==len,字典序排名在给定串前,枚举每一位
22             for(char ch=i?a[i-1]+1:‘a‘;ch<a[i];++ch)//枚举每个小于a[i]的字母
23                 ans+=C[‘z‘-ch][len-i-1];
24         }printf("%d\n",ans);
25     }return 0;
26 }

poj1496/poj1850 Code

原文地址:https://www.cnblogs.com/kafuuchino/p/9882294.html

时间: 2024-08-27 00:18:57

poj1496 Word Index / poj1850 Code(组合数学)的相关文章

POJ1850——Code(组合数学)

Code DescriptionTransmitting 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 t

POJ1496 Word Index【全排列】

题目链接: http://poj.org/problem?id=1496 题目大意: 和给你一个字符串str,输出str在字典序全排列中的位置(从小到大排列). 解题思路: 和 POJ1850 一模一样. 参考博文:http://www.cnblogs.com/lyy289065406/archive/2011/07/31/2122771.html先判断str是不是升序序列,如果是升序序列,则为字典序的第1个,输出1. 不符合第一步的话,则分为两步计算str的字典序位置. 先计算比 str 的长

uva 417 - Word Index(数位dp)

题目连接:uva 417 - Word Index 题目大意:按照题目中的要求,为字符串编号,现在给出字符串,问说编号为多少,注意字符串必须为递增的,否则编号为0. 解题思路:其实就是算说比给定字符串小并且满足递增的串由多少个.dp[i][j]表示第i个位为j满足比给定字符串小并且满足递增的串. dp[i][j]=∑k=0j?1dp[i?1][k]. 注意每次要处理边界的情况,并且最后要加上自身串.并且在处理边界的时候dp[i][0]要被赋值为1,代表前i个为空的情况. #include <cs

UVA 417 - Word Index(数论)

题意:417 - Word Index 题意:每个字符串按题目中那样去映射成一个数字,输入字符串,输出数字 思路:这题还是比较水的,由于一共只有83000多个数字,所以对应一个个数字去映射就可以了,注意字符串进位的情况处理即可 代码: #include <stdio.h> #include <string.h> #include <map> #include <string> using namespace std; char str[10]; map<

POJ1850 Code 【排列组合】

还不算是难题(嘿嘿,因为我做出来了) 很简单,找到相应的方程式解就是了(中间也犯了一个很2的错误,把9+10+11+...+99写成(1+90)*90/2,改过来后就好了) 不多说,code is below #include <iostream> #include <cstdio> #include <cstring> using namespace std; int combi(int a,int b) { int sum=1; int j=1; for(int i

HDU Word Index (数位DP)

题意:给你字符串,算出它的数值: a -> 1 b -> 2 . . z -> 26 ab -> 27 ac -> 28 . . az -> 51 bc -> 52 . . vwxyz -> 83681 字母要求递增. #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #incl

poj1850 Code【组合数学】By cellur925

题意: * 按照字典序的顺序从小写字母 a 开始按顺序给出序列 (序列中都为升序字符串)* a - 1* b - 2* ...* z - 26* ab - 27* ...* az - 51* bc - 52* ...* vwxyz - 83681* 输入字符串由小写字母 a-z 组成字符串为升序,根据字符串输出在字典里的序列号为多少. 很容易地我们可以想到,设输入的字符串长度为len,我们可以用组合数求出所有小于len长度的字符串数量,(感觉这一步很好理解,反而许多题解给出了详细的证明?). 然

POJ 1850 Code 组合数学

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

poj1850(Code)

题目地址:Code 题目大意: 按照字典序的顺序从小写字母a开始按顺序给出序列 a - 1     b - 2     ...     z - 26     ab - 27     ...     az - 51     bc - 52     ...     vwxyz - 83681     ... 输入字符串由小写字母a-z组成字符串为升序,根据字符串输出在字典里的序列号为多少. 解题思路: 排列组合,记住公式: 先看字符的长度一个字符的时候是26为C(26,1),两个字符的时候分别从以a