题意:
输出某个str字符串在字典中的位置。
规定长度小的字符串排在长度大的字符串前面,并且输入的字符串的字符必须是升序排列。不降序列是非法字符串。
由于字典是从a=1开始的,因此str的位置值就是 在str前面所有字符串的个数 +1
对于输入的英文字符串:
1.先判断是否合法,即是否能进行code
2.然后就是要求 编码小于它的个数+1
3.先计算长度小于该字符串的编码数量
假如输入字符串长度为L
那么对于长度len的字符串(1<=len<L)
有C[26][len]个可编码,
因为就是从26个字母里面选出len个不同的字母,然后按照字母顺序有唯一一种排列。
4.然后计算长度等于该字符串,但是字典序小于它的字符串。
对字符串从左往右扫描,假若当前位置字母为'y',上一位的字母为'b' ,
枚举当前位为'c','d','e','f'...'x'的情况(保证了字典序比自己小,并且合法)。对未扫描的位置进行排列组合。
见代码。
5.+1(因为要给自己编号,还要加自身)
Code
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 9226 | Accepted: 4399 |
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.
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.
• 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
Source
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<iomanip>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const int maxn=26 ;
ll ret,C[maxn+3][maxn+3];
int n;
char s[15];
void pre()//打表,杨辉三角,组合数
{
C[0][0]=1;
for(int i=1;i<=maxn;i++)
{
C[i][0]=C[i][i]=1;
for(int j=1;j<i;j++)
{
C[i][j]=C[i-1][j-1]+C[i-1][j];
}
}
}
bool check()//判断是否合法,即是否能进行code
{
for1(i,n-1)
{
if(s[i]>=s[i+1]) return false;
}
return true;
}
ll work()
{
ll ans=0;
for1(i,n-1)//先计算长度小于该字符串的编码数量
{
ans+=C[26][i];
}
for1(i,n)
{
int ind=s[i]-'a'+1;
int st= i==1?1:s[i-1]-'a'+1+1;//这一位至少要比前一位大
for(int j=st;j<=ind-1;j++)//枚举这一位
{
if(n-i>26-j) continue;
ans+=C[26-j][n-i];
}
}
ans++;
return ans;
}
int main()
{
pre();
while(~scanf("%s",s+1))
{
n=strlen(s+1);
if(!check()) {puts("0");continue;}
printf("%lld\n",work());
}
return 0;
}