UVa 12166 Equilibrium Mobile

就贴代码

Code

#include <cstdio>
#include <iostream>
#include <sstream>
#include <map>
#include <algorithm>
using namespace std;
map<long long, int> m;
string s;
void dfs(int l, int r, int d)
{
  if (s[l] == '[')
  {
    int flag = 0;
    for (int i = l + 1; i < r; i++)
    {
      if (s[i] == '[') flag++;
      if (s[i] == ']') flag--;
      if (flag == 0 && s[i] == ',')
      {
        dfs(l + 1, i - 1, d + 1);
        dfs(i + 1, r - 1, d + 1);
      }
    }
  }else
  {
    stringstream ss(s.substr(l, r - l + 1));
    long long t;
    ss >> t;
    m[t << d]++;
  }
}
void solve()
{
  m.clear();
  cin >> s;
  dfs(0, s.size() - 1, 0);
  int maxx = 0;
  long long total = 0;
  for (map<long long, int>::iterator it = m.begin(); it != m.end(); it++)
  {
    total += it->second;
    if (it->second > maxx) maxx = it->second;
  }
  cout << total - maxx << endl;
}
int main()
{
 // freopen("input.txt", "r", stdin);
  int t;
  cin >> t;
  for (int i = 0; i < t; i++)
    solve();
}

你可能感兴趣的:(UVa 12166 Equilibrium Mobile)