The problem is from yesterday's contest from Codeforces.
I have observed that, from the left side (i.e. MSB), for every digit, if we replace the given number with the output of f
when after passing the digit - we get a bigger number we will get the desired output. I'm a beginner in programming and have written code with Python 3, which is showing TLE after submission (though it is correct and gives correct output for the sample test cases). I think a better algorithm is needed.
Variables st
(start index), flag
, cntr
(length of contiguous subsegment ) are taken for the condition the part where we will replace the digits needs to be contiguous subsegment:
n = int(input())
s = input()
f = list(map(int,input().split()))
result = []
st = -1
flag = 1
cntr = 0
for i in range(len(s)):
x=f[int(s[i])-1]
if(x>int(s[i])):
result.append(str(x))
if(flag==1):
st = i
flag = 0
if(flag==0):
cntr+=1
else:
result.append(s[i])
if(st!=-1 and (i-st-1)!=cntr):
break
for j in range(i+1,len(s)):
result.append(s[j])
#print(result)
r = int(''.join(result))
print(r)
Can anyone make any improvement in my code? I know there is a tutorial on that website, but I want to learn how to develop code which I have written already.
1 Answer 1
The speed issue can be fixed by using PyPy 3 as it is faster (at least in most codeforces type problems, I don't know in general)
However this will result in a WA verdict. To fix this just modify the break condition as follows and it will work:
if(st!=-1 and x!=int(s[i])):
This is because the current version can sometimes break out of the loop prematurely when \$x == int(s[i])\$ because it may be better to make the segment longer.
Finally, I consider you are using too many flag variables, only one is required. Here is how I would change the code:
n = int(input())
s = input()
f = list(map(int,input().split()))
result = []
flag = 1
for i in range(len(s)):
x=f[int(s[i])-1]
if(x>int(s[i])):
result.append(str(x))
if(flag==1):
flag = 0
else:
result.append(s[i])
if(flag!=1 and x!=int(s[i])):
break
for j in range(i+1,len(s)):
result.append(s[j])
#print(result)
r = int(''.join(result))
print(r)
-
\$\begingroup\$ Thank you for helping. Not getting why we need to break the loop when
x!=int(s[i])
givenflag!=1
. In every iteration,x
will be loaded withf[int(s[i])-1]
. I used the break statement to check if after starting the replacement we no more have greater number as output. \$\endgroup\$tarit goswami– tarit goswami2019年04月27日 18:53:27 +00:00Commented Apr 27, 2019 at 18:53 -
1\$\begingroup\$ I honestly don't understand what the reasoning behind (i-st-1)!=cntr is. But you just need to break as soon as there is a value such that x<int(s[i]) because then it becomes clear that you shouldn't make the segment that you are applying f to any longer. \$\endgroup\$user62030– user620302019年04月27日 18:58:10 +00:00Commented Apr 27, 2019 at 18:58
-
\$\begingroup\$ Ok, so we can use
x<int(s[i])
. If I usex!=int(s[i])
there, as, every timex
is getting updated byf[int(s[i])-1]
, the loop will break after just replacing one digit, as theflag=0
at that time already. About my program, I am storing the index of the digit, where first replacement occurs, inst
variable. Increasing the var.cntr
everytime when replacement occurs. But when for first time(after replacement has started forst
th digit) when we getx<int(s[i])
thecntr
will not be updated and the value of(i-st-1)
andcntr
will be not same. \$\endgroup\$tarit goswami– tarit goswami2019年04月27日 19:07:18 +00:00Commented Apr 27, 2019 at 19:07 -
\$\begingroup\$ well yeah but the cntr variable isn' very usefull, the only part where you use cntr is in the (i-st-1)!=cntr part. But I dont understand that part. Also, notice that the flag variable isnt used , you can just use the st variable. \$\endgroup\$user62030– user620302019年04月27日 19:09:36 +00:00Commented Apr 27, 2019 at 19:09
-
\$\begingroup\$ Thank you. I wish your channel would be in English :) \$\endgroup\$tarit goswami– tarit goswami2019年04月27日 19:14:45 +00:00Commented Apr 27, 2019 at 19:14
Explore related questions
See similar questions with these tags.