Skip to main content
deleted 51 characters in body; edited tags
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

The problem is from yesterday's contest from Codeforces. Here is the link: https://codeforces.com/contest/1157/problem/Byesterday's contest from Codeforces.

I have observed that, from the left side  (i.e;MSBe. 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 3Python 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. Here is my code, variables

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 codemy code? I know there is a tutorial on that website, but I want to learn how to develop a code which I have written already. Any help will be appreciated.

The problem is from yesterday's contest from Codeforces. Here is the link: https://codeforces.com/contest/1157/problem/B

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. Here is my code, 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 a code which I have written already. Any help will be appreciated.

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.

edited tags
Link
added 191 characters in body
Source Link

The problem is from yesterday's contest from Codeforces. Here is the link: https://codeforces.com/contest/1157/problem/B

I have observed that, from the left side(i.e;msbe;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 a 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. Here is my code, 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 a code which I have written already. Any help will be appreciated.

The problem is from yesterday's contest from Codeforces. Here is the link: https://codeforces.com/contest/1157/problem/B

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 a 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. Here is my code:

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 a code which I have written already. Any help will be appreciated.

The problem is from yesterday's contest from Codeforces. Here is the link: https://codeforces.com/contest/1157/problem/B

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. Here is my code, 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 a code which I have written already. Any help will be appreciated.

Source Link
Loading