UVa 12166 Equilibrium Mobile

本文介绍了一段使用C++进行复杂字符串解析与数据处理的代码,通过递归深度优先搜索算法处理嵌套的方括号表达式,并将解析后的数值左移位运算后存入映射表中,最后计算并输出所有重复数值的总和减去最大重复次数的数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

就贴代码
#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();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值