# 題目
zerojudge k733. 3. 磁軌移動序列
# 解題思路
目前沒東西...
# 程式碼
#include<iostream> | |
using namespace std; | |
string s; | |
int n; | |
struct track{ | |
int top,botton; | |
long long sum; | |
}; | |
track f(int &idx){ | |
int top=-1,point=-1; | |
long long sum=0; | |
while(idx<n && s[idx]!='E'){ | |
if(s[idx]=='T'){ | |
int buffer=stoi(s.substr(idx+1,idx+2)); | |
if(point==-1) top=point=buffer; | |
else{ | |
sum+=abs(point - buffer); | |
point=buffer; | |
} | |
idx+=3; | |
}else if(s[idx]=='L'){ | |
int r=stoi(s.substr(idx+1,idx+1)); | |
idx+=2; | |
track temp=f(idx); | |
if(point==-1){// 判斷還沒讀到值先進入迴圈的情況 | |
top=point=temp.top; | |
sum+=r*temp.sum+(r-1)*abs(temp.botton-temp.top); | |
}else sum+=abs(point-temp.top)+r*temp.sum+(r-1)*abs(temp.botton-temp.top); | |
point=temp.botton; | |
idx++; | |
} | |
} | |
return {top,point,sum}; | |
} | |
int main(){ | |
cin>>s; | |
s="T10"+s; | |
n=s.size(); | |
int idx=0; | |
cout<<f(idx).sum; | |
} |