码迷,mamicode.com
首页 > 其他好文 > 详细

Topcoder SRM 688 div2

时间:2016-05-12 14:35:45      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

Topcoder SRM 688 div2
250:

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

class ParenthesesDiv2Easy {
public:
    int getDepth(string);
};

int ParenthesesDiv2Easy::getDepth(string s) {
    int n = s.size();
    if(n == 0) return 0;
    int cnt = 0;
    int pre = 0;
    int res = 0;
    for(int i = 0 ; i < n ; i++) {
        if(s[i] == ‘)‘) cnt--;
        else if(s[i] == ‘(‘) cnt++;
        if(cnt == 0) {
            res = max(res, getDepth(s.substr(pre + 1, i - pre - 1)) + 1);
            pre = i + 1;
        }
    }
    return res;
}

<%:testing-code%>
//Powered by [KawigiEdit] 2.0!

500:
/*
想了一阵
首先把合法的括号都排除掉
然后剩下的都是不合法的括号

首先采用贪心的变法,遇到只有)没有(与它对应的时候,把)变成(
变换后的就是合法情况,或者左括号多的情况

左括号多的情况,则从后往前变(为),直到总情况合法

分类讨论后发现,总的操作次数是满足题意的。

*/

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

class ParenthesesDiv2Medium {
public:
    vector <int> correct(string);
};

vector <int> ParenthesesDiv2Medium::correct(string s) {
    int n = s.size();
    int vis[100];
    for(int i = 0 ; i < n ; i++) vis[i] = 0;
    stack<int>sta;
    vector<int> res;
    for(int i = 0 ; i < n ; i++) {
        if(s[i] == ‘(‘) sta.push(i);//, printf("i = %d, first\n", i);
        else if(s[i] == ‘)‘ && !sta.empty()) {
            int u = i, v = sta.top(); sta.pop();
//            printf("u = %d,v = %d\n", u,v);
            vis[u] = 1, vis[v] = 1;
//            printf("i = %d, second\n", i);
        }
    }
    vector<int>tres;
    for(int i = 0 ; i < n ; i++) {
        if(vis[i] == 0) tres.push_back(i);
    }

    int cnt = 0;
    for(int i = 0 ; i <(int)tres.size(); i ++) {
    //    printf("tres[%d] = %d\n", i, tres[i]);
        if(s[tres[i]] == ‘(‘) cnt++;
        else cnt--;
        if(cnt < 0) vis[tres[i]] = 1, cnt += 2, res.push_back(tres[i]);
    }
    if(cnt < 0) {
        for(int i = 0 ; i < n ; i++) {
            if(vis[i] == 0 && s[i] == ‘)‘) vis[i] = 1, cnt += 2, res.push_back(i);
            if(cnt >= 0) break;
        }
    }
    if(cnt > 0) {
        for(int i = n - 1 ; i >= 0 ; i--) {
            if(vis[i] == 0 && s[i] == ‘(‘) vis[i] = 1, cnt -= 2, res.push_back(i);
            if(cnt <= 0) break;
        }
    }
    sort(res.begin(), res.end());
    //for(int i = 0  ; i < (int)res.size() ; i++) printf("%d ", res[i]);
    //puts("");
    return res;
}

<%:testing-code%>
//Powered by [KawigiEdit] 2.0!

1000:
/*
把所有字符串按照L和R分成无数个子串,剩下未分入任何子串的字符做一个统计预备替换子串中元素用
那么,每个子串可有两个数字sum1,sum2分别表示左括号不合法和右括号不合法的个数

注意每次替换子串中一个元素,比如(,那么sum1-=2
所以子串应该替换比如(直到sum1<=2,此时若sum1为1的话,可证此时sum2也为1,表示还剩下一对不合法的(和),只要交换他们两个就可以

*/

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

class ParenthesesDiv2Hard {
public:
    int minSwaps(string, vector <int>, vector <int>);
};
const int MAXN = 100 + 5;
int sum1[MAXN], sum2[MAXN], vis[MAXN];
int ParenthesesDiv2Hard::minSwaps(string s, vector <int> L, vector <int> R) {
    int n = s.size(), m = L.size();
    for(int i = 0 ; i <= n ; i++) vis[i] = 0;
    for(int i = 0 ; i < m ; i++) if((R[i] - L[i]) % 2 == 0) return -1;
    for(int i = 0 ; i < m ; i++) {
        int cnt = 0;
        sum1[i] = sum2[i] = 0;
        for(int j = L[i] ; j <= R[i] ; j++) {
            if(s[j] == ‘(‘) cnt++;
            else cnt--;
            if(cnt < 0) cnt++, sum2[i]++;
            vis[j] = 1;
        }
        sum1[i] = max(0, cnt);
    }
    int c1, c2; c1 = c2 = 0;
    for(int i = 0 ; i < n ; i++) {
        if(vis[i] == 1) continue;
        if(s[i] == ‘(‘) c1++;
        else c2++;
    }

    int s1, s2; s1 = s2 = 0;
    int res = 0;
    for(int i = 0 ; i < n ; i++) {
        s1 += sum1[i] / 2;
        s2 += sum2[i] / 2;
        if(sum1[i] % 2) res++;
    }
   // printf("s1 = %d, s2 = %d, c1 = %d, c2 = %d\n", s1, s2, c1, c2);

    if(s1 > s2 && s1 > s2 + c2) return -1;
    else if(s2 > s1 && s2 > s1 + c1) return -1;
    else return res + max(s1, s2);
}

<%:testing-code%>
//Powered by [KawigiEdit] 2.0!

Topcoder SRM 688 div2

标签:

原文地址:http://blog.csdn.net/beihai2013/article/details/51361901

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!