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

topcoder-srm-604-div2

时间:2015-05-13 19:57:44      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:topcoder   dp   

250分:直接暴力切分字符串

/*************************************************************************
    > File Name: 250.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年05月10日 星期日 12时23分09秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

bool vis[55][55];
class FoxAndWord {
    public:
        int howManyPairs(vector <string> str) {
            int size = str.size();
            int ans = 0;
            memset(vis, 0, sizeof(vis));
            for (int i = 0; i < size; ++i) {
                for (int j = i + 1; j < size; ++j) {
                    if (vis[i][j]) {
                        continue;
                    }
                    if (str[i].length() != str[j].length()) {
                        continue;
                    }
                    int len = str[i].length();
                    for (int k = 0; k < len - 1; ++k) {
                        string tmp1 = "";
                        string tmp2 = "";
                        string tmp3 = "";
                        string tmp4 = "";
                        for (int l = 0; l <= k; ++l) {
                            tmp1 += str[i][l];
                        }
                        for (int l = k + 1; l < len; ++l) {
                            tmp2 += str[i][l];
                        }
                        for (int l = 0; l < len - k - 1; ++l) {
                            tmp3 += str[j][l];
                        }
                        for (int l = len - k - 1; l < len; ++l) {
                            tmp4 += str[j][l];
                        }
                        if (tmp4 == tmp1 && tmp2 == tmp3) {
                            ++ans;
                            vis[i][j] = vis[j][i] = 1;
                            break;
                        }
                    }
                }
            }
            return ans;
        }
};

500分:
差不多最多走个20步,所以直接枚举状态,然后存到set里,最后查下就行了

/*************************************************************************
    > File Name: 500.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年05月10日 星期日 12时40分31秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <LL, LL> PLL;

set <PLL> st;
LL POW[25];

class PowerOfThreeEasy {
    public:
        string ableToGet(int X, int Y) {
            if (!X && !Y) {
                return "Possible";
            }
            POW[0] = 1;
            for (int i = 1; i <= 19; ++i) {
                POW[i] = POW[i - 1] * 3;
            }
            st.clear();
            st.insert(make_pair(0LL, 0LL));
            for (int i = 0; i < (1 << 20); ++i) {
                PLL u;
                LL x = 0, y = 0;
                int s = 0;
                for (int j = 19; j >= 0; --j) {
                    if (i & (1 << j)) {
                        s = j;
                        break;
                    }
                }
                for (int j = 0; j <= s; ++j) {
                    if (i & (1 << j)) {
                        y += POW[j];
                    }
                    else {
                        x += POW[j];
                    }
                }
                u.first = x;
                u.second = y;
                st.insert(u);
                swap(u.first, u.second);
                st.insert(u);
            }
            if (st.find(make_pair((LL)X, (LL)Y)) != st.end()) {
                return "Possible";
            }
            return "Impossible";
        }
};

1000分:
简单的树形dp
dp[u][j]u,j
dp[u][j]=dp[u][j]+dp[u][k]?dp[v][j?k]
v是u的儿子节点,初始化是dp[u][1] = 1

/*************************************************************************
    > File Name: 1000.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年05月13日 星期三 15时53分08秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

LL dp[55][55];
int num[55];
const int mod = 1e9 + 7;
vector <int> tree[55];

void DP(int u, int fa) {
    dp[u][1] = 1;
    num[u] = 1;
    int size = tree[u].size();
    for (int i = 0; i < size; ++i) {
        int v = tree[u][i];
        if (v == fa) {
            continue;
        }
        DP(v, u);
        num[u] += num[v];
    }
    for (int i = 0; i < size; ++i) {
        int v = tree[u][i];
        if (v == fa) {
            continue;
        }
        for (int j = num[u]; j >= 1; --j) {
            for (int k = 0; k <= j && k <= num[v]; ++k) {
                dp[u][j] += dp[u][j - k] * dp[v][k];
                dp[u][j] %= mod;
            }
        }
    }
}

class FoxConnection2 {
    public:
        int ways(vector <int> A, vector <int> B, int k) {
            int n = A.size() + 1;
            for (int i = 1; i <= n; ++i) {
                tree[i].clear();        
                num[i] = 0;
            }
            memset(dp, 0, sizeof(dp));
            for (int i = 0; i < n - 1; ++i) {
                int u = A[i];
                int v = B[i];
                tree[u].push_back(v);
                tree[v].push_back(u);
            }
            DP(1, 0);
            int ans = 0;
            for (int i = 1; i <= n; ++i) {
                ans += dp[i][k];
                ans %= mod;
            }
            return ans;
        }
};

topcoder-srm-604-div2

标签:topcoder   dp   

原文地址:http://blog.csdn.net/guard_mine/article/details/45694853

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