SRM 599 D2L3: SimilarNames2,dp

来源:互联网 发布:ntp服务器windows工具 编辑:程序博客网 时间:2024/04/30 03:49

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