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. [windows]win7设置wifi热点

    1.启用并设定虚拟WiFi网卡:netsh wlan set hostednetwork mode=allow ssid=whylaughing key=124025621 2.开启无线wifi网络: ...

  2. java+spring 执行器

    A 通过MethodInvokingJobDetailFactoryBean类实现 spring配置文件里增加执行器配置 <bean id="ammoDue" class=& ...

  3. bat 符号说明

    netstat -an|findstr 139 ipconfig/all findstr IP ipconfig/all |findstr   物理地址             定值选行 ipconf ...

  4. JavaScript的语音识别

    有没有想过给您的网站增添语音识别的功能?比如您的用户不用点鼠标,仅仅通过电脑或者手机的麦克风发布命令,比如"下拉到页面底部",或者"跳转到下一页",您的网站就会 ...

  5. afnetworking NSCocoaErrorDomain Code=3840 解决

    afnetworking json解析出错 解决方法1 AFURLResponseSerialization.m 258行修改 responseString = [responseString str ...

  6. ERROR 14856 --- [reate-882003853] com.alibaba.druid.pool.DruidDataSource : create connection error, url: jdbc:mysql://localhost:3306/xhb?useUnicode=true&characterEncoding=UTF-8, errorCode 1045, sta

    ERROR 14856 --- [reate-882003853] com.alibaba.druid.pool.DruidDataSource : create connection error, ...

  7. 数组初始化 和 vector初始化

    ] = {}; 整个数组都初始化为0 vector<); 整个vector初始化为1 如果你定义的vector是这样定义的: vector<int> B; 去初始化,千万不要用: ; ...

  8. Bootstrap历练实例:按钮组大小

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  9. 稳定性 耗时 gc 过长问题排查 和工具

    自己的另外一篇: http://www.cnblogs.com/fei33423/p/7805186.html 偶有耗时抖动? gc 也有长耗时? fullgc 也是? 有同学反馈 swap 可能导致 ...

  10. H3C交换机系统时间设置漏洞

    H3C交换机系统时间设置存在漏洞 1. 背景说明 由于在编写<主机房网络延伸实施方案>,调试H3C S5120S-28P-EI交换机时,发现交换机设置成现在的时间后,导致本地用户通过ssh ...