Ozon Tech Challenge 2020--B. Kuroni and Simple Strings(1200+构造双指针)

简介: 算法

20.png

题意:要求你从只含有()的字符串中删除一些使得剩下的字符串不会有成括号的情况。

思路:从左边开始找(,从右边开始找),优先匹配,不行则停止。

#include<bits/stdc++.h>
using namespace std;
const int maxn=10005;
int sum1[maxn],sum2[maxn];
char s1[maxn];
int ans1[maxn];
int main()
{
    int n,i,j,t;
    cin>>(s1+1);
    for(i=1;i<strlen(s1+1);i++)
    {
        if(s1[i]=='(')
            sum1[i]=sum1[i-1]+1,sum2[i]=sum2[i-1];
        else
            sum2[i]=sum2[i-1]+1,sum1[i]=sum1[i-1];
    }
    int l=1,r=strlen(s1+1),ans=0,idx=0,cnt=0,flag=0;
    while(l<=r)
    {
        if(flag==0)
        {
            if(s1[l]=='(')
            {
                flag=l;
            }
            l++;
        }
        else
        {
            if(s1[r]==')')
            {
                ans1[cnt++]=flag;
                ans1[cnt++]=r;
                ans++;
                flag=0;
            }
            r--;
        }
        //cout<<l<<" "<<r<<" "<<flag<<" "<<ans<<endl;
    }
    if(ans>0)
    {
        cout<<1<<endl<<2*ans<<endl;
        sort(ans1,ans1+cnt);
        for(i=0;i<cnt;i++) cout<<ans1[i]<<" ";
        cout<<endl;
    }
    else
        cout<<0<<endl;
    return 0;
}
相关文章
|
数据安全/隐私保护 C++ Python
LeetCode 804. Unique Morse Code Words
LeetCode 804. Unique Morse Code Words
52 0
CF1454 E. Number of Simple Paths (基环树 拓扑排序)
CF1454 E. Number of Simple Paths (基环树 拓扑排序)
67 0
|
人工智能 测试技术
PAT (Basic Level) Practice (中文) B1008 数组元素循环右移问题 (20 分)
PAT (Basic Level) Practice (中文) B1008 数组元素循环右移问题 (20 分)
71 0
PAT (Basic Level) Practice (中文) B1008 数组元素循环右移问题 (20 分)
|
测试技术
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
76 0
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
PAT (Basic Level) Practice (中文) B1046 划拳 (15 分)
PAT (Basic Level) Practice (中文) B1046 划拳 (15 分)
58 0
PAT (Basic Level) Practice (中文) 1016 部分A+B (15 分)
PAT (Basic Level) Practice (中文) 1016 部分A+B (15 分)
63 0
|
C语言
PAT (Basic Level) Practice (中文) B1026 程序运行时间 (15 分)
PAT (Basic Level) Practice (中文) B1026 程序运行时间 (15 分)
94 0
|
人工智能 vr&ar
Atcoder--Candy Distribution II--前缀和+map
题目描述 There are N boxes arranged in a row from left to right. The i-th box from the left contains Ai candies. You will take out the candies from some consecutive boxes and distribute them evenly to M children. Such being the case, find the number of the pairs (l,r) that satisfy the following:
77 0
Data Structures and Algorithms (English) - 6-5 Evaluate Postfix Expression(25 分)
Data Structures and Algorithms (English) - 6-5 Evaluate Postfix Expression(25 分)
97 0
Data Structures and Algorithms (English) - 6-4 Reverse Linked List(20 分)
Data Structures and Algorithms (English) - 6-4 Reverse Linked List(20 分)
96 0