思维,模拟-HDU 5920(CCPC2016长春)

题目大意:

给一个数S( S ≤ 1 0 1000 S \leq 10^{1000} S101000).让你分解成不超过五十个 回文数之和.

题目思路:

先讲我的思路:求当前比S小的最大回文串.不停的做大数减法直到 S = 0.

那么这里涉及到一个知识点: 如何快速的求比S小的最大回文串?

其实最暴力的办法就是将S的前半部分翻转接到尾部得到的回文串G.

1.特判:10000 变成99999

2.如果 G < = S G <= S G<=S . 容易证明,G就是最大回文数.由于G和S的前半部分完全相等.所以增大任意一位都会导致 G > S G > S G>S.

3.如果 G > S G > S G>S.那我们自然是需要减小某一位. 即从中间开始往左走,找到第一个非0数位置 i i i,将其-1.然后中间的数全填9. [ 1 , i − 1 ] [1,i-1] [1,i1]照抄.翻转接在尾部得到的回文串G即为最大.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
const int maxn = 1e5 + 5;
string a;
string getmx (string & a)
{
    
    
    if (a.size() == 1) return a;
    string ans = "";
    if (a[0] == '1'){
    
    
        bool ok = true;
        for (int i = 1 ; i < a.size() ; i++) if (a[i] != '0') ok = false;
        if (ok) {
    
    
            for (int i = 1 ; i < a.size() ; i++) ans += '9';
            return ans;
        }
    }
    int n = a.size();
    bool ok = false;
    int pos = -1;
    for (int i = n / 2 - 1; i >= 0 ; i--){
    
    
        if (a[i] == a[n - i - 1]) continue;
        pos = i;
        if (a[i] > a[n - i - 1]){
    
    
            ok = true;
            break;
        }
        else {
    
    
            ok = false;
            break;
        }
    }
    if (pos == -1) return a;
    if (ok){
    
    
        for (int i = 0 ; i < pos ; i++){
    
    
            ans += a[i];
        }
        ans += (char)(a[pos] - 1);
        for (int i = pos + 1 ; i < n / 2 ; i++) ans += '9';
        string tmp = ans;
        reverse(ans.begin() , ans.end());
        ans = tmp + ((n & 1) ? "9" : "") + ans;
    }
    else {
    
    
        for (int i = 0 ; i < n / 2 ; i++) ans += a[i];
        string tmp = ans;
        reverse(ans.begin() , ans.end());
        if (n & 1) ans = tmp + a[n / 2] + ans;
        else ans = tmp + ans;
    }
    return ans;
}
string de (string  a , string b)
{
    
    
    reverse(a.begin() , a.end());
    reverse(b.begin() , b.end());
    string ans;
    int len = max (a.size() , b.size());
    int jw = 0;
    for (int i = 0 ; i < len ; i++){
    
    
        int x = a[i] - '0' , y ;
        if (i < b.size()) y = b[i] - '0';
        else y = 0;
        if (jw) x-- , jw = 0;
        if (x < y){
    
    
            jw = 1;
            x += 10;
            x -= y;
        }else {
    
    
            x -= y;
        }
        ans += to_string(x);
    }
    while (ans.size() > 1 && ans.back() == '0') ans.pop_back();
    reverse(ans.begin(),ans.end());
    return ans;
}
int main()
{
    
    
//    cout << de("908" , "898") << endl;
    vector<string> Q;
    ios::sync_with_stdio(false);
    int t; cin >> t;
    int ca = 0;
    while (t--){
    
    
        Q.clear();
        cin >> a;
        string res;
        int cnt = 0;
        int s = 0;
        while (1){
    
    
            cnt++;
            s++;
            res = getmx(a);
            Q.push_back(res);
            a = de (a , res);
            if (a == "0") break;
        }
        cout << "Case #" << ++ca << ":" << endl;
        cout << cnt << endl;
        for (int i = 0 ; i < Q.size() ; i++)
            cout << Q[i] << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35577488/article/details/109028286