Editorial for Големи числа


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.
by: maya

Тестовете можете да видите тук.

Примерно решение на C++:

#include <iostream>
#include <string>
#include <algorithm>
#include <cstdlib>
using namespace std;
int n;
string s="";

string fff( int i, int j)
{
    string s1="";
    if (i==j) s1=s.substr(i,1);else s1=s.substr(i,2);
    while (1 <= i && j  < n-1 && s[i - 1] == s[j + 1] && s[i-1]>='0' && s[i-1]<='9')
    { s1=s.substr(i-1,1)+s1+s.substr(j+1,1);
        i--;
        j++;
    }
    return s1;
}

bool poli( string s)
{
    string s1=s;
    reverse(s1.begin(),s1.end());
    return s1==s;
}
string big(string s1, string s2)
{
    while ( s2.size()>0 &&s2[0]=='0') s2=s2.substr(1,s2.size()-2);
    if (s1.size()>s2.size()) return s1;
    else if (s1.size()<s2.size())return s2;
    else if ( s1>s2) return s1;
    else return s2;;
}


int main()
{

    cin>>n;
    string a;
    for (int i=1; i<=n; i++)
    {
        cin>>a;
       if(a[0]=='0'){ while(a[0]=='0') a.erase(0,1); if(a=="")a="0";}
           s+=a;
    }
    n=s.size();
    string max="",s1;
    if(poli(s)) cout<<s<<endl;
    else{
    for (int i = 1; i <n; i++)
    {
        // нечетeн палиндром
        s1=fff( i, i);
        max = big(max, s1);

        if (i + 1 < n && s[i + 1] == s[i])
        {
            // четeн палиндром
            s1=fff(i, i+1);
            max = big(max, s1);
        }
    }
    cout<<max<<endl;
    }
    return 0;
}

Comments

There are no comments at the moment.