zoukankan      html  css  js  c++  java
  • POJ 3276 The Cow Lexicon(dp)

    简单dp

    • 假设dp[i]为前i个字符能够最小去除dp[i]个元素,剩下的可以由字符串组成。所以可知

      dp[i] = min(dp[i], dp[i-j] + i - j - len); 其中len为[i-j,i]区间, 存在的字符串的长度。

    • 但是由于,对于每一个i,都要向前面找j。 这样就会很麻烦。 那么只需要倒着求一遍就好了

    #include<iostream>
    #include<string.h>
    #include<vector>
    #include<string>
    #include<algorithm>
    using namespace std;
    
    int dp[607];
    
    int main()
    {
        int w, l;
        cin >> w >> l;
    
        string s;
        cin >> s;
    
        vector<string> str(w);
        for(int i=0; i<w; ++ i)
            cin >> str[i];
    
        memset(dp, 0x3f, sizeof(dp));
    
        dp[l] = 0;
        for(int i = s.size() - 1; i >= 0; -- i)
        {
            dp[i] = dp[i+1] + 1;
            for(size_t j = 0; j < str.size(); ++ j)
            {
                size_t x = i, y = 0;
                while(x < s.size() && y < str[j].size())
                {
                    if(s[x] == str[j][y])
                        x ++, y ++;
                    else
                        x ++;
                }
    
                if(y >= str[j].size())
                    dp[i] = min(dp[i], dp[x] + int(x - i - str[j].size()));
            }
        }
        cout << dp[0] << endl;
        return 0;
    }
    
    
  • 相关阅读:
    架构师图谱
    P3398 仓鼠找sugar
    NOIP 2017小凯的疑惑
    P2568 GCD
    自动AC机
    qbxt国庆刷题班 游记&总结
    【学习笔记】manacher算法
    [ZROI]分组
    BSGS与扩展BSGS
    Crt and ExCrt
  • 原文地址:https://www.cnblogs.com/aiterator/p/6547790.html
Copyright © 2011-2022 走看看