解题一
//转自牛客网
链接:https://www.nowcoder.com/questionTerminal/c9188bc172ff40dc9b8bc1edfe6e3212
来源:牛客网
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
char st[15];
int com(int n, int r)
{
if (n - r < r)
r = n - r;
int i, j, s = 1;
for (i = 0, j = 1; i < r; ++i)
{
s *= (n - i);
for (; j <= r && s % j == 0; ++j)
s /= j;
}
return s;
}
int main()
{
//freopen("t.txt", "r", stdin);
gets(st);
int len = strlen(st);
if (len == 1)
{
printf("%d\n", st[0] - 'a' + 1);
return 0;
}
bool ok = true;
if (st[0] <= 'z' && st[0] >= 'a')
ok = true;
else
ok = false;
for (int i = 1; i < len; i++)
if (!(st[i] <= 'z' && st[i] >= 'a' && st[i] > st[i - 1]))
ok = false;
if (!ok)
{
printf("0\n");
return 0;
}
int ans = 26;
for (int i = 2; i < len; i++)
ans += com(26, i);
ans += com(26, len) - com(26 - (st[0] - 'a'), len);
for (int i = 1; i < len; i++)
ans += com(26 - (st[i - 1] - 'a' + 1), len - i) - com(26 - (st[i] - 'a'
), len - i);
printf("%d\n", ans + 1);
return 0;
}
解题二
//思路看看
题目大意:
按照字典序的顺序从小写字母a开始按顺序给出序列
a - 1
b - 2
...
z - 26
ab - 27
...
az - 51
bc - 52
...
vwxyz - 83681
...
输入字符串由小写字母a-z组成字符串为升序,根据字符串输出在字典里的序列号为多少。
解题思路:
排列组合,记住公式:
先看字符的长度一个字符的时候是26为C(26,1),两个字符的时候分别从以a开头的计算出25个,b开头的有24个...可以计算出C(26,2) 依次三个字符的时候C(26,3)....
所以给出的字符串先算比它长度小的字符串的总和再算和它长度相等的字符串有多少个,算相等的时候注意要始终算它的前一个字符的总和,这样才能保证正确性,算出的总和最后+1.就是这个字符串的序列号。
解题三:
https://blog.csdn.net/lyy289065406/article/details/6648492 //该博客写得比较好理解
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int c[30][30];
char str[12];
void Cmn()
{
c[0][0] = 1;
for(int i = 1; i < 30; i++){
c[i][0] = c[i][i] = 1;
for(int j = 1;j < i; j++){
c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
}
}
}
void solve(){
int sum = 0;
for(int i = 1; i < strlen(str); i++){
sum += c[26][i];
}
for(int i = 0; i < strlen(str); i++){
char ch = (!i)?'a':str[i - 1] + 1;
while(ch <= str[i] - 1){
sum += c['z' - ch][strlen(str) - i - 1];
ch++;
}
}
sum ++;
cout<<sum <<endl;
}
int main()
{
Cmn();
cin>> str;
for(int i = 1; i < strlen(str); i++){
if(str[i] <= str[i - 1]){
cout<< 0<<endl;
return 0;
}
}
solve();
}
解题四
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int c[30][30];
char in[15];
bool judge(char a[], int len)
{
for(int i=1; i<len; i++)
if(a[i]<a[i-1]) return true;
for(int i=0; i<len; i++)
for(int j=0; j<i-1; j++)
if(a[i]==a[j]) return true;
return false;
}
int main()
{
for(int i=1; i<=27; i++)
{
c[i][0] = 1;
for(int j=1; j<=i; j++)
c[i][j] = (i-j+1)*c[i][j-1]/j;
}
while(scanf("%s", in+1) == 1)
{
in[0] = 96;
int len = strlen(in+1);
if(judge(in+1, len))
{
printf("0\n");
}
else
{
int res = 0;
for(int i=1; i<=len-1; i++) res += c[26][i];
for(int i=1; i<=len; i++)
{
for(int j=in[i]-'a'+1-1; j>in[i-1]-'a'+1; j--)
res += c[26-j][len-i];
}
printf("%d\n", res+1);
}
}
//printf("%d", 'a'-1);
return 0;
}