topcoder-srm-604-div2
2015-05-13 16:29
489 查看
250分:直接暴力切分字符串
500分:
差不多最多走个20步,所以直接枚举状态,然后存到set里,最后查下就行了
1000分:
简单的树形dp
dp[u][j]表示以u为根的子树,取出连通的j个节点的方案数dp[u][j] 表示 以u为根的子树,取出连通的j个节点的方案数
则dp[u][j]=dp[u][j]+dp[u][k]∗dp[v][j−k]则dp[u][j] = dp[u][j] + dp[u][k] * dp[v][j - k]
v是u的儿子节点,初始化是dp[u][1] = 1
/************************************************************************* > 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] 表示 以u为根的子树,取出连通的j个节点的方案数
则dp[u][j]=dp[u][j]+dp[u][k]∗dp[v][j−k]则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 Div1 第1题
- TopCoder SRM 609 DIV2 500points
- topcoder srm 686 div1 -3
- TopCoder SRM 144 DIV1 550 Lottery
- Topcoder SRM 643 Div1 250<peter_pan>
- TopCoder 250 points 9-SRM 148 DIV 1 75/250 30%
- TopCoder 350 points 21-SRM 154 DIV 1 105/350 30%
- TopCoder SRM 558 DIV2 Level 3:CatAndRabbit
- TopCoder SRM 649 Div2 Problem 1000 - XorSequenceEasy (思维)
- topcoder SRM 591 DIV2 TheArithmeticProgression
- Topcoder SRM 709 DIV 2 500pt Permatchd2 solution
- [TopCoder] SRM_594_DIV2.250
- topcoder srm 710 div1 -23
- TopCoder SRM 601 div2
- Topcoder SRM 636 Div2 1000(切蛋糕,最后一块给自己。最小值中求最大值,二分+枚举)
- TopCoder SRM 144 DIV2(550-point)
- TopCoder SRM 144 DIV 2
- topcoder srm 714 div1 -3
- Topcoder SRM 413 (Div 2) 1000.InfiniteSequence
- topcoder srm 663 div1 -23