题解:P13049 [GCJ 2020 Qualification] Nesting Depth
题目传送门
题意
给定一个数字字符串
思路
我们可以把 (,增加深度直到等于 ),减少深度直到等于 )闭合所有剩余的括号。
AC CODE
#include <bits/stdc++.h>
using namespace std;
int t;
int main() {
cin>>t;
for (int i=1;i<=t;i++) {
string s;
cin>>s;
string new_s;
int shen=0;
for (int j=0;j<s.size();j++) {
int n=s[j]-'0';
while(shen<n) {
new_s+='(';
shen++;
}
while(shen>n) {
new_s+=')';
shen--;
}
new_s+=s[j];
}
while(shen>0) {
new_s+=')';
shen--;
}
cout<<"Case #"<<i<<": "<<new_s<<endl;
}
return 0;
}