Problem:
Problem J:
Substring Expression
Trees are sometimes represented in the form of strings. Here is one of the most popular ways to represent unlabeled trees:
Leaves are represented by "
()
".
Other nodes (i.e. internal nodes) are represented by "
(
S
1
S
2
...
S
n
)
", where
S
i
is the string representing the
i
-th subnode.
For example, the tree depicted in the figure below is represented by a string "
((()())())
".
A strange boy Norward is playing with such strings. He has found that a string sometimes remains valid as the representation of a tree even after one successive portion is removed from it. For example, removing the underlined portion from the string "
((()
())(
))
" results in "
((()))
", which represents the tree depicted below.
However, he has no way to know how many ways of such removal there are. Your task is to write a program for it, so that his curiosity is fulfilled.
Input
The input contains a string that represents some unlabeled tree. The string consists of up to 100,000 characters.
Output
Print the number of portions of the given string such that removing them results in strings that represent other valid trees.
Sample Input 1
((()())())
Output for the Sample Input 1
10

Submitted solutions:

code_snippet: #include<iostream>
#include<unordered_map>

int main(){
  std::unordered_map<long long,long long> counter;
  std::string line;
  int tmp = 0;
  getline(std::cin,line);
  for(int i=0;i<(int)line.size();i++){
    tmp += ((line[i]=='(')?+1:-1);
    if(tmp)counter[tmp]++;
  }
  long long ans = 0;
  for(std::pair<long long,long long> iter : counter)ans += (iter.second*(iter.second-1))/2;
  std::cout << ans << std::endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3232
final_rank: 1

code_snippet: #include<iostream>
#include<map>

int main(){
  std::map<long long,long long> counter;
  std::string line;
  int tmp = 0;
  getline(std::cin,line);
  for(int i=0;i<(int)line.size();i++){
    tmp += ((line[i]=='(')?+1:-1);
    if(tmp)counter[tmp]++;
  }
  long long ans = 0;
  for(std::pair<long long,long long> iter : counter)ans += (iter.second*(iter.second-1))/2;
  std::cout << ans << std::endl;
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4408
final_rank: 4

code_snippet: #include<iostream>
#include<map>

int main(){
  std::map<int,int> counter;
  std::string line;
  int tmp = 0;
  getline(std::cin,line);
  for(int i=0;i<(int)line.size();i++){
    tmp += ((line[i]=='(')?+1:-1);
    if(tmp)counter[tmp]++;
  }
  long long ans = 0;
  for(std::pair<int,int> iter : counter)ans += (iter.second*(iter.second-1))/2;
  std::cout << ans << std::endl;
  return 0;
}
accuracy: 0.9375
time_ms: 20
memory_kb: 3620
final_rank: 6

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

const int MAX = 100005;
vector<int> V[MAX];
int depth[MAX];

int main(){
  
  for(int i = 0; i < MAX; i++) V[i].clear();
  string in;
  getline(cin , in);
  string s = "";
  for(int i = 0; i < (int)in.length(); i++) if(in[i] != ' ') s += in[i];

  V[0].push_back(0);
  depth[0] = 0;
  int p = 0;
  for(int i = 0; i < (int)s.length(); i++){
    if(s[i] == '(') p++;
    if(s[i] == ')') p--;
    V[p].push_back(i+1);
    depth[i+1] = p;
  }

  long long ans = 0;
  for(int i = 1; i < (int)s.length(); i++)
    ans += V[depth[i]].end()-upper_bound(V[depth[i]].begin(), V[depth[i]].end(), i);
  cout << ans << endl;

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 5696
final_rank: 2

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

const int MAX = 100005;
vector<int> V[MAX];
int depth[MAX];

int main(){
  
  for(int i = 0; i < MAX; i++) V[i].clear();
  string s;
  cin >> s;
  V[0].push_back(0);
  depth[0] = 0;
  int p = 0;
  for(int i = 0; i < (int)s.length(); i++){
    if(s[i] == '(') p++;
    if(s[i] == ')') p--;
    V[p].push_back(i+1);
    depth[i+1] = p;
  }

  int ans = 0;
  for(int i = 1; i <= (int)s.length(); i++)
    ans += V[depth[i]].end()-upper_bound(V[depth[i]].begin(), V[depth[i]].end(), i);
  cout << ans << endl;

  return 0;
}
accuracy: 0.9375
time_ms: 10
memory_kb: 5596
final_rank: 5

code_snippet: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

int main(){
  string s;
  cin>>s;
  map<int, int> cnt;
  int cur = 0;
  ll ans = 0;
  REP(i, s.size()){
    if(s[i] == ')') cur++;
    else cur--;
    ans += cnt[cur];
    cnt[cur]++;
  }
  cout << ans << endl;
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3380
final_rank: 3