C - Cheapest Palindrome

Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow an electronic ID tag that the system will read as the cows pass by a scanner. Each ID tag's contents are currently a single string with length M (1 ≤ M ≤ 2,000) characters drawn from an alphabet of N (1 ≤ N ≤ 26) different symbols (namely, the lower-case roman alphabet).

Cows, being the mischievous creatures they are, sometimes try to spoof the system by walking backwards. While a cow whose ID is "abcba" would read the same no matter which direction the she walks, a cow with the ID "abcb" can potentially register as two different IDs ("abcb" and "bcba").

FJ would like to change the cows's ID tags so they read the same no matter which direction the cow walks by. For example, "abcb" can be changed by adding "a" at the end to form "abcba" so that the ID is palindromic (reads the same forwards and backwards). Some other ways to change the ID to be palindromic are include adding the three letters "bcb" to the begining to yield the ID "bcbabcb" or removing the letter "a" to yield the ID "bcb". One can add or remove characters at any location in the string yielding a string longer or shorter than the original string.

Unfortunately as the ID tags are electronic, each character insertion or deletion has a cost (0 ≤ cost ≤ 10,000) which varies depending on exactly which character value to be added or deleted. Given the content of a cow's ID tag and the cost of inserting or deleting each of the alphabet's characters, find the minimum cost to change the ID tag so it satisfies FJ's requirements. An empty ID tag is considered to satisfy the requirements of reading the same forward and backward. Only letters with associated costs can be added to a string.

Input

Line 1: Two space-separated integers: N and M 
Line 2: This line contains exactly M characters which constitute the initial ID string 
Lines 3.. N+2: Each line contains three space-separated entities: a character of the input alphabet and two integers which are respectively the cost of adding and deleting that character.

Output

Line 1: A single line with a single integer that is the minimum cost to change the given name tag.

Sample Input

3 4
abcb
a 1000 1100
b 350 700
c 200 800

Sample Output

900

Hint

If we insert an "a" on the end to get "abcba", the cost would be 1000. If we delete the "a" on the beginning to get "bcb", the cost would be 1100. If we insert "bcb" at the begining of the string, the cost would be 350 + 200 + 350 = 900, which is the minimum.
这个你先要明白删除一个字符和删除一个字符的操作是一样的,全部理解为删除好了
我们用dp[i][j]表示将i~j位置的字符串变为回文串的最低耗费。
可得以下递推关系:
当str[i]==str[j]时:d[i][j]=d[i+1][j-1]
前一个状态肯定是回文
 d[i][j] = min{ d[i+1][j]+value[i], d[i][j-1]+value[j] } ;    
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
char s[];
int dp[][];
int a[];
int main() {
int n,m;
while(cin>>n>>m){
getchar();
scanf("%s",s+);
memset(dp,,sizeof(dp));
for(int i=;i<n;i++){
getchar();
char c=getchar();
int e,f;
cin>>e>>f;
a[(int)c]=min(e,f);
}
for(int i=m;i>;i--)
for(int j=i+;j<=m;j++){
if(s[i]==s[j])dp[i][j]=dp[i+][j-];
else dp[i][j]=min(dp[i+][j]+a[(int)s[i]],dp[i][j-]+a[(int)s[j]]);
}
cout<<dp[][m]<<endl; }
return ;
}

D - A Mini Locomotive

A train has a locomotive that pulls the train with its many passenger coaches. If the locomotive breaks down, there is no way to pull the train. Therefore, the office of railroads decided to distribute three mini locomotives to each station. A mini locomotive can pull only a few passenger coaches. If a locomotive breaks down, three mini locomotives cannot pull all passenger coaches. So, the office of railroads made a decision as follows:

1. Set the number of maximum passenger coaches a mini locomotive can pull, and a mini locomotive will not pull over the number. The number is same for all three locomotives. 
2. With three mini locomotives, let them transport the maximum number of passengers to destination. The office already knew the number of passengers in each passenger coach, and no passengers are allowed to move between coaches. 
3. Each mini locomotive pulls consecutive passenger coaches. Right after the locomotive, passenger coaches have numbers starting from 1.

For example, assume there are 7 passenger coaches, and one mini locomotive can pull a maximum of 2 passenger coaches. The number of passengers in the passenger coaches, in order from 1 to 7, is 35, 40, 50, 10, 30, 45, and 60.

If three mini locomotives pull passenger coaches 1-2, 3-4, and 6-7, they can transport 240 passengers. In this example, three mini locomotives cannot transport more than 240 passengers.

Given the number of passenger coaches, the number of passengers in each passenger coach, and the maximum number of passenger coaches which can be pulled by a mini locomotive, write a program to find the maximum number of passengers which can be transported by the three mini locomotives.

Input

The first line of the input contains a single integer t (1 <= t <= 11), the number of test cases, followed by the input data for each test case. The input for each test case will be as follows: 
The first line of the input file contains the number of passenger coaches, which will not exceed 50,000. The second line contains a list of space separated integers giving the number of passengers in each coach, such that the i th number of in this line is the number of passengers in coach i. No coach holds more than 100 passengers. The third line contains the maximum number of passenger coaches which can be pulled by a single mini locomotive. This number will not exceed 1/3 of the number of passenger coaches. 

Output

There should be one line per test case, containing the maximum number of passengers which can be transported by the three mini locomotives.

Sample Input

1
7
35 40 50 10 30 45 60
2

Sample Output

240

一个数列,n个数,找三个k个连续数的子数列,使其和最大。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int f[][],a[];
int main() {
int t;
scanf("%d",&t);
while(t--) {
int n;
scanf("%d",&n);
a[]=;
for(int i=; i<=n; i++) {
scanf("%d",&a[i]);
a[i]+=a[i-];
}
int m;
scanf("%d",&m);
memset(f,,sizeof(f));
for(int i=; i<=n; i++)
for(int j=; j<; j++) {
int k=i-m;
if(k<) k=;
f[i][j]=max(f[i-][j],f[k][j-]+a[i]-a[k]);
}
printf("%d\n",f[n][]);
}
return ;
}

dp的两个不错的题的更多相关文章

  1. 推荐两个不错的CAD二次开发(.Net)手册

    推荐两个不错的CAD二次开发(.Net)手册 http://www.mjtd.com/helpcenter/netguide/index.html http://www.ceesky.com/book ...

  2. 两个不错的IT类优质号

    虽然标题已经被用烂了,但是我觉得还是用这样的方式介绍这两个不错的公众号,可能你们刚好需要,我刚好知道,仅此而已. 刚认识的一个小哥哥和一个小姐姐,他们都非常优秀,有喜欢Java和Linux的同学千万不 ...

  3. 两道不错的递推dp

    hdoj-4055 #include <cstdio> #include <cstring> #include <iostream> #include <al ...

  4. 树形dp入门两题

    题目描述 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题.一天他早晨骑车去上课,路上见到一个老伯正在修剪花花草草,顿时想到了一个有关修剪花卉的问题.于是当日课后,小明 ...

  5. FJOI2020 的两道组合计数题

    最近细品了 FJOI2020 的两道计数题,感觉抛开数据范围不清还卡常不谈里面的组合计数技巧还是挺不错的.由于这两道题都基于卡特兰数的拓展,所以我们把它们一并研究掉. 首先是 D1T3 ,先给出简要题 ...

  6. 数位dp初步——数位dp的两种方式

    数位dp:一类统计区间[L,R]内某种符合规定的数字个数的题目.特征是R的范围会很大,O(N)范围内无法完成. 一般而言,解决这类题目有两种方式,一种是递推,另一种是记忆化搜索. 递推: 1)利用dp ...

  7. 木棍加工(dp,两个参数的导弹拦截问题)

    题目描述 一堆木头棍子共有n根,每根棍子的长度和宽度都是已知的.棍子可以被一台机器一个接一个地加工.机器处理一根棍子之前需要准备时间.准备时间是这样定义的:     第一根棍子的准备时间为1分钟:   ...

  8. Codevs1378选课[树形DP|两种做法(多叉转二叉|树形DP+分组背包)---(▼皿▼#)----^___^]

    题目描述 Description 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了N(N<300)门的选修课程,每个学生可选课程的数量M是给定的.学生选修 ...

  9. 两道相似KMP题

    1.POJ 3450 Coporate Identity 这两题的解法都是枚举子串,然后匹配,像这种题目以后可以不用KMP来做,直接字符串自带的strstr函数搞定,如果字符串未出现,该函数返回NUL ...

随机推荐

  1. laravel 错误总结

    1.ReflectionException (-1) Class PhotosController does not exist 原因: 资源路由的问题 ,命名空间要区分大小写,admin首字母要大写 ...

  2. 精通AngularJS(三)深入scope,继承结构,事件系统和生命周期

    深入探讨 Scope 作用域 每一个 $scope 都是类 Scope 的一个实例.类 Scope 拥有可以控制 scope 生命周期的方法,提供事件传播的能力,并支持模板渲染. 作用域的层次结构 让 ...

  3. Java生成-zipf分布的数据集(自定义倾斜度,用作spark data skew测试)

    1.代码 import java.io.Serializable; import java.util.NavigableMap; import java.util.Random; import jav ...

  4. MySQL字符集和排序介绍

    客服端字符集: character_set_client utf8mb4连接字符集: character_set_connection utf8mb4数据库字符集: character_set_dat ...

  5. hihoCoder #1162 : 骨牌覆盖问题·三 (矩阵快速幂,DP)

    题意:有一个k*n的棋盘,要求用1*2的骨牌来铺满,有多少种方案?(k<8,n<100000001) 思路: 由于k是比较小,但是又不那么小,可以专门构造这样的一个矩阵M,使得只要我们有一 ...

  6. SublimeREPL配置Python3开发

    首先什么是REPL? A Read-Eval-Print-Loop (REPL) is available both as a standalone program and easily includ ...

  7. Could not load OpenSSL解决

    问题 Could not load OpenSSL. You must recompile Ruby with OpenSSL support or change the sources in you ...

  8. Codeforces Round #316 (Div. 2) C Replacement 扫描法

    先扫描一遍得到每个位置向后连续的'.'的长度,包含自身,然后在扫一遍求出初始的合并次数. 对于询问,只要对应位置判断一下是不是'.',以及周围的情况. #include<bits/stdc++. ...

  9. Thread源码分析-java8

    1.Thread特性分析 守护线程Daemon 定性:支持性线程,主要用于程序中后台调度以及支持性工作. 当JVM中不存在Daemon线程时,JVM将会退出. 将一个线程设定为Daemon的方法: 调 ...

  10. WPF知识点全攻略10- 路由事件

    路由事件是WPF不得不提,不得不会系列又一 先来看一下他的定义: 功能定义:路由事件是一种可以针对元素树中的多个侦听器(而不是仅针对引发该事件的对象)调用处理程序的事件. 实现定义:路由事件是一个 C ...