D1. The World Is Just a Programming Task (Easy Version) (字符串匹配)

本文解析了Codeforces竞赛中编号为1248的D1题,通过定义字符串的美丽值并使用C++实现了一个算法,该算法计算字符串的最大美丽值,并找到交换两个字符以获得最大美丽值的位置。

http://codeforces.com/contest/1248/problem/D1

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int>pi;
ll beauty(string &s)
{
   int n = s.size();
   ll veg = 1;
   ll ob = 0, pc = 0;
   for(int i = 0; i < n; i++)
   {
    if(s[i] == '(')
        ob++;
    else
        ob--;

        if(ob == pc)
        {
            veg++;
        }
        else if(ob < pc)
        {

            pc = ob;
            veg = 1;
        }
   }
   if(ob != 0)
    return 0;
   else if(pc == 0)
    return veg-1;
   else
    return veg;

}

int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.precision(20);
  int n;
  cin >> n;
string s;
cin >> s;
ll res = beauty(s), ib = 0, jb = 0;
for(int i = 0 ; i< n; i++)
{
    for(int j = i + 1; j < n; j++)
    {

        swap(s[i] , s[j]);
        ll c = beauty(s);
        swap(s[i],s[j]);
        if(c > res)
        {

            res = c;
            ib = i;
            jb = j;
        }
    }
}
cout << res << endl;
cout << ib +1 << " "<< jb + 1 <<endl;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值