F - Transpose
https://atcoder.jp/contests/abc350/tasks/abc350_f
思路
开辟数组,记录左右括号配对的位置值, 例如:
串:a(s)l
位置: 01234
数组值为
配对数组: 03010
数组构成方法为 使用stack 记录左括号位置,遇到右括号时候,将当前位置值记录为 stack top值
基于此数组,使用dfs递归处理s。
Code
string s; int match[500005]; void dfs(int start, int end, bool casetoggle, bool directtoggle){ if (!directtoggle){ for(int i=start; i<=end; i++){ if (s[i] == '('){ dfs(i+1, match[i]-1, !casetoggle, !directtoggle); i = match[i]; } else { char oppo = s[i]; if (casetoggle){ bool isupper = s[i]<='Z'; int offset = s[i] - (isupper?'A':'a'); oppo = offset + (isupper?'a':'A'); } cout << (char)oppo; } } } else { for(int i=end; i>=start; i--){ if (s[i] == ')'){ dfs(match[i]+1, i-1, !casetoggle, !directtoggle); i = match[i]; } else { char oppo = s[i]; if (casetoggle){ bool isupper = s[i]<='Z'; int offset = s[i] - (isupper?'A':'a'); oppo = offset + (isupper?'a':'A'); } cout << (char)oppo; } } } } int main() { cin >> s; s = " " + s; int start = 1; int end = s.size()-1; stack<int> st; for(int i=1; i<=end; i++){ if (s[i] == '('){ st.push(i); } else if (s[i] == ')'){ int leftpos = st.top(); st.pop(); match[i] = leftpos; match[leftpos] = i; } } dfs(start, end, false, false); return 0; }
标签:int,Transpose,dfs,start,casetoggle,数组 From: https://www.cnblogs.com/lightsong/p/18149250