SRM 599 D2L3: SimilarNames2,dp
2014-02-17 14:43
429 查看
题目:http://community.topcoder.com/stat?c=problem_statement&pm=12871&rd=15711
dp[i][j] 表示 当前已选择前 i 个字符串,且最后一个字符串为 names[j]。
代码:
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <bitset>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
#define CHECKTIME() printf("%.2lf\n", (double)clock() / CLOCKS_PER_SEC)
/*************** Program Begin **********************/
const int MOD = 1000000007;
int dp[55][55];
class SimilarNames2 {
public:
int L, n;
vector <string> names;
bool isPrefix(string s, string mathch)
{
if (s.size() < mathch.size()) {
return false;
} else {
return ( mathch == s.substr(0, mathch.size()) );
}
}
int rec(int cur, int s)
{
if (cur == L) {
return 1;
}
int & res = dp[cur][s];
if (res != -1) {
return res;
}
res = 0;
for (int i = 0; i < n; i++) {
if (i != s && isPrefix(names[i], names[s])) {
res += rec(cur + 1, i);
res %= MOD;
}
}
return res;
}
int count(vector <string> names, int L) {
this->L = L;
this->names = names;
this->n = names.size();
memset(dp, -1, sizeof(dp));
long long res = 0;
for (int i = 0; i < n; i++) {
res += rec(1, i);
res %= MOD;
}
for (int i = 1; i <= n - L; i++) {
res *= i;
res %= MOD;
}
return res;
}
};
/************** Program End ************************/
dp[i][j] 表示 当前已选择前 i 个字符串,且最后一个字符串为 names[j]。
代码:
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <bitset>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
#define CHECKTIME() printf("%.2lf\n", (double)clock() / CLOCKS_PER_SEC)
/*************** Program Begin **********************/
const int MOD = 1000000007;
int dp[55][55];
class SimilarNames2 {
public:
int L, n;
vector <string> names;
bool isPrefix(string s, string mathch)
{
if (s.size() < mathch.size()) {
return false;
} else {
return ( mathch == s.substr(0, mathch.size()) );
}
}
int rec(int cur, int s)
{
if (cur == L) {
return 1;
}
int & res = dp[cur][s];
if (res != -1) {
return res;
}
res = 0;
for (int i = 0; i < n; i++) {
if (i != s && isPrefix(names[i], names[s])) {
res += rec(cur + 1, i);
res %= MOD;
}
}
return res;
}
int count(vector <string> names, int L) {
this->L = L;
this->names = names;
this->n = names.size();
memset(dp, -1, sizeof(dp));
long long res = 0;
for (int i = 0; i < n; i++) {
res += rec(1, i);
res %= MOD;
}
for (int i = 1; i <= n - L; i++) {
res *= i;
res %= MOD;
}
return res;
}
};
/************** Program End ************************/
相关文章推荐
- [树形DP][状压DP] SRM599 950-point SimilarNames
- SRM 609 D2L3: VocaloidsAndSongs,dp
- SRM 572 D2L3:DistinctRemainders,dp,math
- SRM 602 D2L3:BlackBoxDiv2,dp
- SRM 620 D2L3: RandomGraph, dp
- SRM 591 D2L3:YetAnotherTwoTeamsProblem,dp
- SRM 612 D2L3:PowersOfTwo,dp
- SRM 610 D2L3:MiningGoldEasy,dp
- SRM 620 D2L3: RandomGraph, dp
- SRM 604 D2L3:FoxConnection2,dp
- SRM 615 D2L3:MergeStrings,dp
- SRM 627 D2L3: BubbleSortWithReversals, dp, 冒泡排序
- SRM 440 WickedTeacher (DP)
- [DP] Topcoder SRM 552 DIV1 Hard. HolyNumbers
- [树形DP] SRM 598 Div1 Hard TPS
- SRM 599 div2 250 500
- TopCoder SRM 648 Div2 Problem 1000 - ABC (DP)
- SRM 599 1A 2013.12.6
- Topcoder srm 653 div.2 1000 - SingingEasy(区间DP)
- srm 306 div2 1000 (字符串dp,进阶)