首页 > 其他分享 >Move Brackets(贪心+思维)

Move Brackets(贪心+思维)

时间:2022-11-17 22:13:25浏览次数:72  
标签:num sequence Brackets Move int bracket test regular 贪心

题目链接

题目描述:

You are given a bracket sequence \(s\) of length \(n\), where \(n\) is even (divisible by two). The string \(s\) consists of \(\frac{n}{2}\) opening brackets '(' and \(\frac{n}{2}\) closing brackets ')'.

In one move, you can choose exactly one bracket and move it to the beginning of the string or to the end of the string (i.e. you choose some index \(i\), remove the \(i\)-th character of \(s\) and insert it before or after all remaining characters of \(s\)).

Your task is to find the minimum number of moves required to obtain regular bracket sequence from \(s\). It can be proved that the answer always exists under the given constraints.

Recall what the regular bracket sequence is:

"( )" is regular bracket sequence;
if \(s\) is regular bracket sequence then "(" + \(s\) + ")" is regular bracket sequence;
if \(s\) and \(t\) are regular bracket sequences then \(s + t\) is regular bracket sequence.
For example, "( ) ( )", "( ( ) ) ( )", "( ( ) )" and "( )" are regular bracket sequences, but ") (", "( ) (" and ") ) )" are not.

You have to answer \(t\) independent test cases.

输出描述:

The first line of the input contains one integer \(t\) (\(1≤t≤2000\)) — the number of test cases. Then \(t\) test cases follow.

The first line of the test case contains one integer \(n\) (\(2≤n≤50\)) — the length of \(s\). It is guaranteed that \(n\) is even. The second line of the test case containg the string \(s\) consisting of \(\frac{n}{2}\) opening and \(\frac{n}{2}\) closing brackets.

输出描述:

For each test case, print the answer — the minimum number of moves required to obtain regular bracket sequence from \(s\). It can be proved that the answer always exists under the given constraints.

题解:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

int T = 1;
int n, m, x;
int a[N];
int b[N];

void solve()
{
    cin >> n;

    string s;

    cin >> s;

    int ans = 0, num = 0;

    for (int i = 0; i < n; i++) // 贪心的想,若想要移动次数最少,则需要把已经是符合条件的找出来,则剩下的不符合条件的就需要移动
    {
        if (s[i] == '(') // 从前往后找出 (
            num++;
        else
        {
            num--; // 若为 ) 则将 ( 的数量减一

            if (num < 0) // 若减一之后的 ( 的数量大于等于0,则代表有 ( 和 ) 匹配,否则则没有
            {
                ans++; // num 小于0则代表这个 ) 要移动

                num = 0; // 随后要置为零,以防影响之后的判断
            }
        }
    }

    cout << ans << '\n';
}

int main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    // scanf("%d", &T);
    cin >> T;

    while (T--)
    {
        solve();
    }

    return 0;
}

标签:num,sequence,Brackets,Move,int,bracket,test,regular,贪心
From: https://www.cnblogs.com/KSzsh/p/16901152.html

相关文章

  • std::move() 学习
    转自:https://stackoverflow.com/questions/28595117/why-can-we-use-stdmove-on-a-const-object1.右值int a = 10;左值是有固定的内存地址,&a即左值的地址,我们可以把&......
  • 3B(小小贪心)
    题目链接题目大意:有一辆载重量为v的货车,准备运送两种物品。物品A的重量为1,物体B的重量为2,每个物品都有一个价值。求货车可以运送的物品的最大价值。输入格......
  • 代码随想录第三十六天|贪心算法
    今天继续是贪心算法  435.无重叠区间classSolution{publicinteraseOverlapIntervals(int[][]intervals){intn=intervals.length;......
  • 代码随想录第三十五天 | 贪心算法
     第三十五天,继续贪心 860.柠檬水找零 classSolution{publicbooleanlemonadeChange(int[]bills){intn=bills.length;if(bills[0......
  • Qt QSqlDatabase::removeDatabase()遇到的问题
    在Qt中想要关闭数据库,并删除连接时发现出错,代码如下:1if(db.isOpen())2{3QStringconnection;4connection=db.connectionName();5db.close();6......
  • removeIf用法
    删除list中包含某个字符的对象//创建一个动态数组ArrayList<String>sites=newArrayList<>();sites.add("Taobao");//删除名称中带有Tao的元素sites.remove......
  • 20221115_T3A+_贪心二分
    题意你在和Yazid做游戏。Yazid给了你一棵\(n\)个节点的树,并让你删除这棵树上的恰好\(k\)条边,使得整棵树被分成\(k+1\)个连通块。你觉得太简单了,随便删k条边......
  • 代码随想录第三十四天|贪心算法
    今天继续贪心算法,重点是学习贪心算法的思维 1005.K次取反后最大化的数组和 classSolution{publicintlargestSumAfterKNegations(int[]nums,intk){......
  • [LeetCode] 947. Most Stones Removed with Same Row or Column
    Ona2Dplane,weplace n stonesatsomeintegercoordinatepoints.Eachcoordinatepointmayhaveatmostonestone.Astonecanberemovedifitshareseit......
  • 1710. 卡车上的最大单元数 ----- 贪心算法,自定义sort排序
    请你将一些箱子装在一辆卡车上。给你一个二维数组boxTypes,其中boxTypes[i]=[numberOfBoxesi,numberOfUnitsPerBoxi]:numberOfBoxesi是类型i的箱子的数量。numb......