zoukankan      html  css  js  c++  java
  • 洛谷1026(字符串dp)

    常规dp。看到数据很小就直接暴力了,没有预处理。kmp好像过分了……

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <bitset>
    using namespace std;
    
    int n, k, t, dp[205][45];
    string T = "#", S[10];
    int Next[10][30];
    
    void Get_Next(string str, int id) {
    	Next[id][1] = 0;
    	for (int i = 2, j = 0; i < str.length(); i++) {
    		while (j && str[j + 1] != str[i])	j = Next[id][j];
    		if (str[j + 1] == str[i])	j++;
    		Next[id][i] = j;
    	}
    }
    
    void Get_f(bitset<205>& bst, string s, int id, string T) {
    	int n = s.length() - 1;
    	for (int i = 1, j = 0; i < T.length(); i++) {
    		while (j && (j == n || T[i] != s[j + 1]))	j = Next[id][j];
    		if (T[i] == s[j + 1])	j++;
    		if (j == n)	bst[i - n + 1] = 1;
    	}
    }
    
    int calc(string str) {
    	bitset<205> bst;
    	bst.reset();
    	for (int i = 0; i < t; i++) {
    		Get_f(bst, S[i], i, str);
    	}
    	return bst.count();
    }
    
    int main() {
    	cin >> n >> k;
    	for (int i = 0; i < n; i++) {
    		string s;
    		cin >> s;
    		T += s;
    	}
    	cin >> t;
    	for (int i = 0; i < t; i++) {
    		cin >> S[i];
    		S[i] = '#' + S[i];
    		Get_Next(S[i], i);
    	}
    	for (int i = 1; i < T.length(); i++) {
    		for (int j = 1; j <= k; j++) {
    			for (int p = j; p < i; p++) {
    				dp[i][j] = max(dp[i][j], dp[p - 1][j - 1] + calc(T.substr(p - 1, i - p + 2)));
    			}
    		}
    	}
    	cout << dp[T.length() - 1][k] << '
    ';
    	return 0;
    }
    
  • 相关阅读:
    SpringBoot整合jsp
    SpringBoot常用application.properties配置
    SpringBoot入门
    vue cli创建vue项目
    vue 指令
    vue hello
    pytest doc
    atom
    java csvutil
    Django uuidfield 实现自动生成唯一列,并设置为主键
  • 原文地址:https://www.cnblogs.com/AlphaWA/p/10772991.html
Copyright © 2011-2022 走看看