Aizu2130-Billion Million Thousand-dp

用dp求出最大的表达,再用dp求出。//然而并没有想出来

 1 #include <cstdio>
 2 #include <string>
 3 #include <algorithm>
 4 #include <iostream>
 5
 6 using namespace std;
 7
 8 const int INF = 0x3f3f3f3f;
 9 const int maxn = 2000;
10
11 int N,v[maxn],len[maxn];
12 int dp[maxn];
13
14 string s[maxn],str;
15
16 int main()
17 {
18     int cas = 1;
19     while(cin >> N && N)
20     {
21         for(int i=0;i<N;i++)
22         {
23             cin >> s[i] >> v[i];
24             len[i] = s[i].length();
25         }
26         cin >> str;
27         int length = str.length();
28
29         for(int i=0;i<=length;i++) dp[i] = -INF;
30         dp[0] = 0;
31
32         for(int i=0;i<=length;i++)
33         {
34             for(int j=0;j<N;j++)
35             {
36                 int End = i - len[j];
37                 if(End>=0 && dp[End]>=0 && str.substr(End,len[j]) == s[j])
38                     {dp[i] = max(dp[i],dp[End]+v[j]);}
39             }
40         }
41
42         int mx = dp[length];
43
44         for(int i=0;i<=mx;i++) dp[i] = INF;
45         dp[0] = 0;
46
47         for(int i=0;i<N;i++)
48         {
49             for(int j=0;j<=mx-v[i];j++)
50                 dp[j+v[i]] = min(dp[j+v[i]] , dp[j]+len[i]);
51         }
52         cout << "Case "<<cas++<< ": " << dp[mx] << endl;
53     }
54 }
时间: 2024-12-15 00:19:35

Aizu2130-Billion Million Thousand-dp的相关文章

Integer to English Words 解答

Question Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. For example, 123 -> "One Hundred Twenty Three" 12345 -> "Twelve Thousand Three Hundred Forty Five" 12

[email&#160;protected] [273] Integer to English Words (String &amp; Math)

https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. For example, 123 -> "One Hundred Twenty Three" 12345 -> "Tw

根据76大细分词性对单词进行归组(二)

词性的重要性不言而喻,尤其是对于自然语言处理来说,哪怕就是记单词,根据词性对单词进行归组也是非常有帮助的. superword是一个Java实现的英文单词分析软件,主要研究英语单词音近形似转化规律.前缀后缀规律.词之间的相似性规律等等. 各大词性及其包括的词: 32.N-COUNT-COLL(可数集合名词) (词数:50) 1 aristocracy army array audience band 2 cast chapter command commission committee 3 co

使用千位分隔符(逗号)表示web网页中的大数字

做手机端页面我们常常遇到数字,而在Safari浏览器下这些数字会默认显示电话号码,于是我们就用到了补坑的方法加入<meta>标签: <meta name="format-detection" content="telephone=no">这个标签的意义在于将数字不被看成电话号码,于是就万事大吉了O(∩_∩)O,但是如果是一个订餐电话,那么在加入这个标签之后,订餐电话就变成了数字,就不能直接拨号了于是乎我们想到了用千位符老表示数字从而区分电话号

HDU 4169 树形DP

Wealthy Family Problem Description While studying the history of royal families, you want to know how wealthy each family is. While you have various 'net worth' figures for each individual throughout history, this is complicated by double counting ca

poj 3252 Round Numbers(数位DP)

Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11003   Accepted: 4064 Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors',

poj3252Round Numbers非递归数位dp解

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9894   Accepted: 3569 Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro, Sham, Bo'

POJ3252---Round Numbers(数位dp)

Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro, Sham, Bo', and a host of other names) in order to make arbitrary decisions such as who gets

HDU 3555 Bomb(数位DP)

Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro, Sham, Bo', and a host of other names) in order to make arbitrary decisions such as who gets

HDU 3366 Passage (概率DP)

Passage Problem Description Bill is a millionaire. But unfortunately he was trapped in a castle. There are only n passages to go out. For any passage i (1<=i<=n), Pi (0<=Pi<=1) denotes the probability that Bill will escape from this castle saf