1616B - Mirror in the String - CodeForces Solution


greedy strings *1100

Please click on ads to support us..

Python Code:

te=int(input())
while(te>0):
	l=int(input())
	s=input()
	e=''
	fm=0
	flag=0
	i=0
	pos=-1
	for i in range(l-1):
		if ord(s[i])>ord(s[i+1]):
			if s[i]!='a':
				fm=1
				pos=i+1
		elif s[i+1]==s[i] and fm==1:
				pos=i+1
		else:
			pos=i
			break
	a=''
	len=(i+1)*2
	i=0
	for i in range(pos+1):
		a=a+s[i]
	k=pos+1
	t=pos
	while(t>=0):
		a=a+a[t]
		t=t-1
	if l==1:
		a=s+s
	print(a)
	te=te-1		

C++ Code:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;cin>>n;
        string s;cin>>s;
        int k=1;
        while((s[k-1]>s[k] && k<n) || (k>1 && s[k]==s[k-1]) ){
            k++;
        }
        for(int i=0;i<k;i++){
            cout<<s[i];
        }
        for(int i=k-1;i>=0;i--){
            cout<<s[i];
        }cout<<endl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku