[PYTHON] AtCoder Beginner Contest 122 Review of past questions

Time required

スクリーンショット 2020-01-29 23.13.54.png

Impressions

The last problem was that I knew that experimentation was important or essential, but I couldn't make the solution easier.

Problem A

Output appropriately using the ternary operator.

answerA.py


b=input()
print("A" if b=="T" else "T" if b=="A" else "G" if b=="C" else "C")

B problem

If any of the AGCT comes, add it, and if any other character comes, return it to zero again.

answerB.py


x=["A","G","C","T"]
ans=0
now=0
for i in input():
    if i in x:
        now+=1
    else:
        now=0
    ans=max(now,ans)
print(ans)

C problem

In this case we use the cumulative sum because we have to process the interval query at the end. However, please note that when you find AC, you have to add +1 to the index ** with ** C.

answerC.py


n,m=map(int,input().split())
n,q=map(int,input().split())
s=input()
x=[0]*n

for i in range(n-1):
    if s[i:i+2]=="AC":
        x[i+1]=x[i]+1
    else:
        x[i+1]=x[i]
for i in range(q):
    l,r=map(int,input().split())
    print(x[r-1]-x[l-1])

D problem

Here, if the AGC character string does not appear up to the i-th character even if the two adjacent characters are exchanged, if the AGC character string does not appear for the character string including the i + 1 character, up to i + 1 It can be said that the AGC string does not appear. Therefore, it seems that the answer will be sought by ** thinking in order from the front **. Here, considering a character string in which a sub-character string called AGC appears, five patterns ** AGC, ACG, GAC, A? GC, AG? C, ** can be considered (the latter two are blind spots). Ta ...). Therefore, we started thinking from the front, and when a substring that fits the 5 patterns appeared, we did not include it in the enumeration. Here, ** 5 pattern substrings are all 4 or less in length, so when considering the i-th character, it is only necessary to know the i-1, i-2, i-3 characters **. Therefore, record the sub-character string of the i-3 to i-1 characters in the dictionary, and when adding each character of A, G, C, D to the ** i character, the sub-characters that apply to the 5 patterns. You can find the correct answer by updating the dictionary value ** only when the column does not appear. Also, at this time, it is necessary to record in another dictionary once instead of the original dictionary. (I tried to write 64 ways without using a dictionary for this problem, but it was too crazy ... ** I should think more abstractly. **)

answerD.py


n=int(input())
alph=["A","C","G","T"]
a=[alph[i]+alph[j]+alph[k] for i in range(4) for j in range(4) for k in range(4)]
d1={l:0 for l in a}
for i in d1:
    if i!="AGC" and i!="ACG" and i!="GAC":
        d1[i]=1
d2={l:0 for l in a}
for i in range(n-3):
    for j in d1:
        next=[j+alph[k] for k in range(4)]
        for k in range(4):
            l=next[k]
            if l[1:]=="GAC" or l[1:]=="ACG" or l[1:]=="AGC":
                pass
            elif l[0]=="A" and l[3]=="C" and (l[1]=="G" or l[2]=="G"):
                pass
            else:
                d2[l[1:]]+=d1[j]
    for j in d1:
        d1[j]=d2[j]
        d2[j]=0
print(sum(d1.values())%1000000007)

Recommended Posts

AtCoder Beginner Contest 102 Review of past questions
AtCoder Beginner Contest 072 Review of past questions
AtCoder Beginner Contest 085 Review of past questions
AtCoder Beginner Contest 062 Review of past questions
AtCoder Beginner Contest 113 Review of past questions
AtCoder Beginner Contest 074 Review of past questions
AtCoder Beginner Contest 051 Review of past questions
AtCoder Beginner Contest 127 Review of past questions
AtCoder Beginner Contest 119 Review of past questions
AtCoder Beginner Contest 151 Review of past questions
AtCoder Beginner Contest 075 Review of past questions
AtCoder Beginner Contest 054 Review of past questions
AtCoder Beginner Contest 110 Review of past questions
AtCoder Beginner Contest 070 Review of past questions
AtCoder Beginner Contest 105 Review of past questions
AtCoder Beginner Contest 112 Review of past questions
AtCoder Beginner Contest 089 Review of past questions
AtCoder Beginner Contest 069 Review of past questions
AtCoder Beginner Contest 079 Review of past questions
AtCoder Beginner Contest 056 Review of past questions
AtCoder Beginner Contest 087 Review of past questions
AtCoder Beginner Contest 067 Review of past questions
AtCoder Beginner Contest 093 Review of past questions
AtCoder Beginner Contest 046 Review of past questions
AtCoder Beginner Contest 123 Review of past questions
AtCoder Beginner Contest 049 Review of past questions
AtCoder Beginner Contest 078 Review of past questions
AtCoder Beginner Contest 081 Review of past questions
AtCoder Beginner Contest 060 Review of past questions
AtCoder Beginner Contest 104 Review of past questions
AtCoder Beginner Contest 057 Review of past questions
AtCoder Beginner Contest 121 Review of past questions
AtCoder Beginner Contest 126 Review of past questions
AtCoder Beginner Contest 090 Review of past questions
AtCoder Beginner Contest 103 Review of past questions
AtCoder Beginner Contest 061 Review of past questions
AtCoder Beginner Contest 059 Review of past questions
AtCoder Beginner Contest 044 Review of past questions
AtCoder Beginner Contest 048 Review of past questions
AtCoder Beginner Contest 124 Review of past questions
AtCoder Beginner Contest 116 Review of past questions
AtCoder Beginner Contest 097 Review of past questions
AtCoder Beginner Contest 088 Review of past questions
AtCoder Beginner Contest 092 Review of past questions
AtCoder Beginner Contest 099 Review of past questions
AtCoder Beginner Contest 065 Review of past questions
AtCoder Beginner Contest 094 Review of past questions
AtCoder Beginner Contest 063 Review of past questions
AtCoder Beginner Contest 107 Review of past questions
AtCoder Beginner Contest 071 Review of past questions
AtCoder Beginner Contest 082 Review of past questions
AtCoder Beginner Contest 084 Review of past questions
AtCoder Beginner Contest 068 Review of past questions
AtCoder Beginner Contest 058 Review of past questions
AtCoder Beginner Contest 043 Review of past questions
AtCoder Beginner Contest 098 Review of past questions
AtCoder Beginner Contest 114 Review of past questions
AtCoder Beginner Contest 120 Review of past questions
AtCoder Beginner Contest 108 Review of past questions
AtCoder Beginner Contest 106 Review of past questions
AtCoder Beginner Contest 122 Review of past questions