博客园 首页 私信博主 显示目录 隐藏目录 管理 动画

Codility:Titanium 2016 challenge:BracketsRotation

发现codility上很难找到自己的代码,所以来存一下。

用的一种水法,不知道是结论对还是数据水。

处理出所有极大合法串最后就只剩)))((((状的括号,然后枚举右端点,左端点单调。

但是未匹配点数量为奇数时有点尴尬,我直接把答案减1水过去了,似乎总是能找到一个极大合法串中的括号修改后匹配多出来的这个括号?

 

#define MN 300001
int st[MN],top,mmh=0,w[MN];
inline int max(int a,int b){return a>b?a:b;}
int solution(string &S, int K) {
    top=0;
    int i;
    for (i=0;S[i];i++){
        w[i]=S[i]==')';
        if (top&&!w[st[top]]&&w[i]) top--;else st[++top]=i;
    }
    if (top==0) return i;
    int L=0,a[2]={0,0};st[0]=-1;st[top+1]=i;
    for (i=1;i<=top;i++){
        a[w[st[i]]]++;
        while (((a[0]+1)/2+(a[1]+1)/2)>K&&L<i) a[w[st[++L]]]--;
        mmh=max(st[i+1]-st[L]-1-((a[0]+a[1])&1),mmh);
    }
    return mmh;
}
View Code

 

posted @ 2017-04-27 10:57  swm_sxt  阅读(328)  评论(0编辑  收藏  举报