そーすにっき

なんかいろいろのせておくばしょ

AOJ1188

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1188

Hierarchical Democracy

ICPC2013のC問題

解法:やるだけ

括弧が閉じるたびに下階層から表を集めて上に持っていく作業をする

sstreamが案外便利かもしれない気がしてきた

#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>

using namespace std;

int parseint(string s){
  stringstream ss(s);
  int r;
  ss >> r;
  return r;
}

int sumdiv2(vector<int> &a){
  int r=0;
  sort(a.begin(),a.end());
  for(int i=0;i<a.size()/2+1;i++){
    r+=a[i]/2+1;
  }
  return r;
}

int sum2(vector<int> &a){
  int r=0;
  sort(a.begin(),a.end());
  for(int i=0;i<a.size()/2+1;i++){
    r+=a[i];
  }
  return r;
}

int main(){
  int n;
  int stack;
  cin >> n;
  string s;

  vector<vector<int> > votes;
  while(n--){
    votes.clear();
    stack = 0;
    int max = 0;
    cin >> s;
    while(s[max]=='[')max++;
    votes.resize(max+3);
    for(int i=0;i<s.size();i++){
      if(s[i]=='[')stack++;
      else if(s[i]==']'){
        stack--;
        if(!votes[stack+2].empty()){
          if(stack+2==max){
            votes[stack+1].push_back(sumdiv2(votes[stack+2]));
          }else{
            votes[stack+1].push_back(sum2(votes[stack+2]));
          }
          votes[stack+2].clear();
        }
      }else{
        int r = parseint(s.substr(i));
        votes[stack].push_back(r);
        while(r/10){r/=10;i++;}
      }
    }
    cout << votes[1][0] << endl;
  }
}