Create a gist now

Instantly share code, notes, and snippets.

JAG 2016 C
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <string>
#define repd(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T>
inline void output(T a, int p) {
if(p) cout << fixed << setprecision(p) << a << "\n";
else cout << a << "\n";
}
// end of template
string dfs(string s, string t){
if (s.size() == 0 || t.size() == 0) { // どちらかの根付き木に要素がない場合は終了
return "";
}
// s
int sdepth = 1;
int spos = 1; // 左の子の右端のカッコの位置を出す
while (sdepth) {
if (s[spos] == '(') {
sdepth++;
}
if (s[spos] == ')') {
sdepth--;
}
spos++;
}
int lspos = spos;
spos++;
int snum = 0;
while (s[spos] != ']') {
snum *= 10;
snum += s[spos] - '0';
spos++;
}
spos++;
// t
int tdepth = 1;
int tpos = 1;
while (tdepth) {
if (t[tpos] == '(') {
tdepth++;
}
if (t[tpos] == ')') {
tdepth--;
}
tpos++;
}
int ltpos = tpos;
tpos++;
int tnum = 0;
while (t[tpos] != ']') {
tnum *= 10;
tnum += t[tpos] - '0';
tpos++;
}
tpos++;
string mid = '[' + to_string(snum + tnum) + ']';
string l = dfs(s.substr(1, lspos - 2), t.substr(1, ltpos - 2));
string r = dfs(s.substr(spos + 1, s.size() - spos - 2), t.substr(tpos + 1, t.size() - tpos - 2));
return '(' + l + ')' + mid + '(' + r + ')';
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
// source code
string A, B;
cin >> A >> B;
string ret = dfs(A, B);
output(ret, 0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment