您的位置:首页 > 其它

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 ************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: