51nod 1385凑数字(字符串+构造)

题目大意:

给定一个n,要求找出一个最短的字符串S,使得所有1到n的整数都是S的子序列。

比如n=10,那么S=”1234056789”的时候,是满足条件的。这个时候S的长度是10。

现在给出一个n,要求输出最短S的长度。

题解:

只需要看最高位和后面所有位的关系即可

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e5;
typedef long long LL;
char s[maxn];
int main()
{
    cin>>s;
    int l = strlen(s);
    LL ans = (l-1)*10;
    int f = 0;
    for(int i = 1; i < l ; i++)
        if(s[i] > s[0]) { f = 0; break; }
        else if(s[i] == s[0]) continue;
        else { f = 1; break; }
    ans += s[0] - '0';
    ans -= f;
    cout<<ans<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/Saurus/p/7592412.html