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;
}
相关文章
CF1454 E. Number of Simple Paths (基环树 拓扑排序)
CF1454 E. Number of Simple Paths (基环树 拓扑排序)
67 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-4 Reverse Linked List(20 分)
Data Structures and Algorithms (English) - 6-4 Reverse Linked List(20 分)
96 0
Data Structures and Algorithms (English) - 6-17 Shortest Path [4](25 分)
Data Structures and Algorithms (English) - 6-17 Shortest Path [4](25 分)
90 0
Data Structures and Algorithms (English) - 6-11 Shortest Path [2](25 分)
Data Structures and Algorithms (English) - 6-11 Shortest Path [2](25 分)
103 0
Data Structures and Algorithms (English) - 6-16 Shortest Path [3](25 分)
Data Structures and Algorithms (English) - 6-16 Shortest Path [3](25 分)
83 0
Data Structures and Algorithms (English) - 6-11 Shortest Path [1](25 分)
Data Structures and Algorithms (English) - 6-11 Shortest Path [1](25 分)
90 0
Data Structures and Algorithms (English) - 7-9 Huffman Codes(30 分)
Data Structures and Algorithms (English) - 7-9 Huffman Codes(30 分)
81 0
Data Structures and Algorithms (English) - 7-8 File Transfer(25 分)
Data Structures and Algorithms (English) - 7-8 File Transfer(25 分)
84 0
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
Data Structures and Algorithms (English) - 6-13 Topological Sort(25 分)
86 0