The last problem was that I knew that experimentation was important or essential, but I couldn't make the solution easier.
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")
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)
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])
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