1626B - Minor Reduction - CodeForces Solution


greedy strings *1100

Please click on ads to support us..

Python Code:

t=int(input())
while(t):
	x=input()
	l=len(x)
	if(l==1):
		print(x)
	elif(l==2):
		print(int(x[0])+int(x[1]))
	else:
		L=[]
		m=18
		M=0
		for i in range(l-1):
			m=min(m,int(x[i])+int(x[i+1]))
			M=max(M,int(x[i])+int(x[i+1]))
			if(int(x[i])+int(x[i+1])>=10):
				L.append([i,int(x[i])+int(x[i+1])])
		if(M<10):
			print(str(int(x[0])+int(x[1]))+x[2:])
		else:
			i=L[-1][0]
			print(x[:i]+str(int(x[i])+int(x[i+1]))+x[i+2:])

	t-=1

C++ Code:

#include<bits/stdc++.h>
#define ll long long
#define tt int ttt; cin >> ttt;   while(ttt--)
#define vt vector<int>
#define nl "\n"
#define mtt for(auto i:mp)
#define mt for(auto &i:a) cin>>i;
#define vl vector<ll>
#define sor sort(v.begin(),v.end(),greater<>())
#define so sort(a,a+n,greater<>())
#define MARWA ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
using namespace std ;
const ll N = 1000000007;
int main() {
    MARWA
    tt{
        string s; cin>>s; bool f=0;
        for (int i = s.size()-1; i >0 ; i--) {
            int x=s[i]-'0',y=s[i-1]-'0';
            if(x+y>9){
                f=1; int b=x+y;
                string m= to_string(b);
                 s[i-1]=m[0]; s[i]=m[1];
                break;
            }
        }
        if(f) cout<<s<<nl;
        else{
            int x=s[0]-'0',y=s[1]-'0';
            int z=x+y; string v= to_string(z);
            s.erase(0, 1);
            s[0]=v[0];
            cout<<s<<nl;
        }
    }
}


Comments

Submit
0 Comments
More Questions

465A - inc ARG
1260A - Heating
1740D - Knowledge Cards
1028A - Find Square
1370B - GCD Compression
888D - Almost Identity Permutations
1106C - Lunar New Year and Number Division
849B - Tell Your World
1650E - Rescheduling the Exam
899C - Dividing the numbers
1023C - Bracket Subsequence
1579D - Productive Meeting
1749E - Cactus Wall
1682C - LIS or Reverse LIS
578A - A Problem about Polyline
1038E - Maximum Matching
1141C - Polycarp Restores Permutation
1685A - Circular Local MiniMax
432B - Football Kit
10C - Digital Root
743B - Chloe and the sequence
909D - Colorful Points
1283C - Friends and Gifts
605B - Lazy Student
1687B - Railway System
61D - Eternal Victory
588B - Duff in Love
12C - Fruits
447B - DZY Loves Strings
325A - Square and Rectangles