AGC029A-python

  1. n=input()
  2. l=[]
  3. i=0
  4. while True:
  5. try:
  6. char_index = n.index('W', i, len(n))
  7. l.append(char_index + 1)
  8. i= char_index + 1
  9. except:
  10. break
  11. sum=0
  12. for j in range(1,len(l)+1):
  13. sum += (l[j-1]-j)
  14. print(sum)

 

最終的にi個のWのインデックスは0,1...i-1となる。

また、Wをiからjに移動するためには(j-i)回の移動が必要

 

 

ABC251B-python

n,w=map(int, input().split())
s=set()
l=list(map(int, input().split()))
l.insert(0,0)
l.insert(0,0)
a=len(l)
for i in range(a):
  for j in range(i+1, a):
    for k in range(j+1,a):
      b=l[i]+l[j]+l[k]
      if b <= w:
        s.add(b)
print(len(s))

 

愚直に数え上げるならforループの塊3ついる...?と思ったが、A1~Anの前に0,0置くことで1つのおもり・2つのおもりのパターンもケアできることに気づいたエライ

ABC065B-python

n=int(input())
l=[0]
for i in range(n):
  l.append(int(input()))
count=0
botton=1
for i in range(n):
  botton=l[botton]
  count += 1
  if botton == 2:
    break
if botton == 2:
  print(count)
else:
  print(-1)

 

i=a[i]みたいな問題、よく見かけるけど自分で1からコーディングするのは初めてだったので存外苦労した。