1181B - Split a Number - CodeForces Solution


greedy implementation strings *1500

Please click on ads to support us..

Python Code:

n = int(input())
s = input()
mx = n
ans = 10**n
for i in range(0,n-1):
    if s[i+1]!='0':
        mx = min(mx,max(i+1,n-(i+1)))
for i in range(0,n-1):
    if(s[i+1]!='0' and max(i+1,n-(i+1))==mx):
        ans = min(ans,int(s[0:i+1])+int(s[i+1:n]))
print(ans)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
string stringgen(string a,string b)
{
    string res="";
    int carry=0,sum=0;
    string s="";
    if(a.length()<b.length())
    {
        for(int i=0;i<b.length()-a.length();i++)
        s+='0';
        a=s+a;
    }
    else if(a.length()>b.length())
    {
        for(int i=0;i<a.length()-b.length();i++)
        s+='0';
        b=s+b;
    }
    for(int i=a.length()-1;i>=0;i--)
    {
         int sum=(a[i]-'0'+b[i]-'0')+carry;
         carry=sum/10;
         res+=to_string(sum%10);
    }
    if(carry!=0)
    res+=to_string(carry);
    reverse(res.begin(),res.end());
    return res;
    
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
            int l,x,y;
            cin>>l;
            string s;
            cin>>s;
            string a,b;
            if(l%2==0)
            {
                x=l/2-1;
                y=l/2;
            }
            else
            {
                x=l/2-1;
                y=l/2+1;
            }
                for(int i=x;i>=0;i--)
                {
                    if(s[i]!='0')
                    {
                        if(i-1>=0)
                        {
                            //cout<<s.substr(0,i)<<" "<<s.substr(i,l-i)<<endl;
                            a=stringgen(s.substr(0,i),s.substr(i,l-i));
                            break;
                        }
                        else
                        {
                            a=stringgen("",s.substr(i,l-i));
                            break;
                        }
                    }
                }
                for(int i=y;i<l;i++)
                {
                    if(s[i]!='0')
                    {
                        if(i-1>=0)
                        {
                            b=stringgen(s.substr(0,i),s.substr(i,l-i));
                            break;
                        }
                        else
                        {
                            b=stringgen("",s.substr(i,l-i));
                            break;
                        }
                    }
                }
                if(b.length()==0)
                b=a;
                if(l%2==0)
                {
                    if(a.length()==b.length())
                        cout<<min(a,b)<<endl;
                   else if(a.length()<b.length())
                    cout<<a<<endl;
                   else
                    cout<<b<<endl;
                }
                else
                {
                    if(s[l/2]=='0')
                    {
                        if(a.length()==b.length())
                        cout<<min(a,b)<<endl;
                        else if(a.length()<b.length())
                        cout<<a<<endl;
                        else
                        cout<<b<<endl;
                    }
                    else
                    {
                        a=stringgen(s.substr(0,l/2 +1),s.substr(l/2+1,l/2));
                        b=stringgen(s.substr(0,l/2),s.substr(l/2,l/2 +1));
                        //cout<<b<<endl;
                        if(a.length()==b.length())
                        cout<<min(a,b)<<endl;
                       else if(a.length()<b.length())
                       cout<<a<<endl;
                      else
                       cout<<b<<endl;
                    }
                }
                
        
}


Comments

Submit
0 Comments
More Questions

1508B - Almost Sorted
1690C - Restoring the Duration of Tasks
1055A - Metro
1036D - Vasya and Arrays
1139C - Edgy Trees
37A - Towers
353A - Domino
409H - A + B Strikes Back
1262A - Math Problem
158C - Cd and pwd commands
194A - Exams
1673B - A Perfectly Balanced String
1104B - Game with string
1169B - Pairs
1567D - Expression Evaluation Error
78A - Haiku
1287A - Angry Students
1428A - Box is Pull
234B - Reading
581B - Luxurious Houses
1481C - Fence Painting
935A - Fafa and his Company
22A - Second Order Statistics
1720B - Interesting Sum
1720A - Burenka Plays with Fractions
3A - Shortest path of the king
1720C - Corners
574A - Bear and Elections
352B - Jeff and Periods
1244A - Pens and Pencils