SRM 599 D2L3: SimilarNames2,dp

题目: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 ************************/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值