longpo的回文
啊...比赛的时候输入打错了,结束之后还照着题解把DP部分重构了一遍然而还是WA...样例都没过,然后直接输了-1
明显的DP...而且数据范围这么小,显然怎么搞都可以...
而且这样的回文的DP是很经典的DP啊
f[i][j]表示从i到j所需要最少的价格
$$f[i][j]=\begin{cases}f[i+1][j-1]&& \text{s[i]=s[j]}\\min\{f[i+1][j]+cst[s[i]],f[i][j-1]+cst[a[j]],f[i+1][j-1]+dis[a[i]][a[j]]\}&& \text{}\end{cases}$$
其中cst[i],dis[i][j]分别表示把i消掉(变成回文)和把i变成j所要的最小代价
有点恶心的是...add erase change可以组合起来形成新的操作
比如说change a->b等价于erase a再add b,显然我们要进行分类讨论
不难发现有这些情况:
$$\begin{cases}把a删掉再加上b& \text{}\\加上a再换成b&& \text{}\\把a变成b再删掉 &&\text{}\\在a后面再加一个a &&\text{}\\直接删掉a &&\text{}\end{cases}$$
而且这些操作互相之间是有联系的...所以操作顺序要改一下
然后因为可能会有a->b->c->d之类的路径存在,我们可以跑Floyd来求得最终的dis[i][j]
然后就可以愉快的DP了,注意填表顺序(有点像区间DP)...
#include<cstdio>
#include<queue>
#include<iostream>
#include<cstring>
#define int long long
using namespace std;
inline int read(){
int ans=,f=;char chr=getchar();
while(!isdigit(chr)){if(chr=='-') f=-;chr=getchar();}
while(isdigit(chr)){ans=(ans<<)+(ans<<)+chr-;chr=getchar();}
return ans*f;
}int n,m,dis[][],era[],add[],f[][],cst[];
char s[],opt[],kk[],ccc[];
signed main(){//比赛的时候读入读错了...自闭
scanf("%s",s+);
for(register int i=;i<=;i++) cst[i]=add[i]=era[i]=1e14;
for(register int i=;i<=;i++) for(int j=;j<=;j++)dis[i][j]=1e14;
n=strlen(s+);m=read();
for(register int i=,x;i<=m;++i){
scanf("%s%s",opt,kk);
if(opt[]=='c'){
scanf("%s",ccc);
x=read();
dis[kk[]][ccc[]]=min(x,dis[kk[]][ccc[]]);
}else if(opt[]=='e'){
x=read();
era[kk[]]=min(era[kk[]],x);
}else{
x=read();
add[kk[]]=min(add[kk[]],x);
}
}
for(register int i='a';i<='z';i++) dis[i][i]=;
for(register int k='a';k<='z';k++)
for(register int i='a';i<='z';i++)
for(register int j='a';j<='z';j++)
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
for(register int i='a';i<='z';i++)
for(register int j='a';j<='z';++j){
cst[i]=min(cst[i],min(add[i],era[i])),
cst[i]=min(cst[i],dis[i][j]+min(era[j],add[j])),
cst[i]=min(cst[i],add[j]+dis[j][i]);
for(register int k='a';k<='z';++k)
cst[i]=min(cst[i],dis[i][j]+add[k]+dis[k][j]);
}
for(register int i=n;i>=;i--){
for(register int j=i+;j<=n;++j){f[i][j]=1e14;
if(s[i]==s[j]) f[i][j]=f[i+][j-];
f[i][j]=min(f[i][j],f[i+][j]+cst[s[i]]);
f[i][j]=min(f[i][j],f[i][j-]+cst[s[j]]);
f[i][j]=min(f[i][j],f[i+][j-]+min(dis[s[j]][s[i]],dis[s[i]][s[j]]));
for(int k='a';k<='z';++k)
f[i][j]=min(f[i][j],f[i+][j-]+dis[s[i]][k]+dis[s[j]][k]);
}
}
if(f[][n]==1e14) cout<<-;else cout<<f[][n];
return ;
}
longpo的回文的更多相关文章
- bzoj 1236: longpo的回文
1236: longpo的回文 题目描述 一个字符串如果从左到右和从右到左读的结果是一样的,我们称之为回文串.现在给定一个字符串,我们有三种操作: 1. 添加一个字母在任何位置(可以在首尾添加 ...
- LeetCode[5] 最长的回文子串
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Palindrome Pairs 回文对
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
随机推荐
- keep
简介 什么是keepalived呢?keepalived是实现高可用的一种轻量级的技术手段,主要用来防止单点故障(单点故障是指一旦某一点出现故障就会导致整个系统架构的不可用)的发生.之所以说keepa ...
- Vue2 + Koa2 实现后台管理系统
看了些 koa2 与 Vue2 的资料,模仿着做了一个基本的后台管理系统,包括增.删.改.查与图片上传. 工程目录: 由于 koa2 用到了 async await 语法,所以 node 的版本需要至 ...
- LVS集群的三种工作模式
LVS的三种工作模式: 1)VS/NAT模式(Network address translation) 2)VS/TUN模式(tunneling) 3)DR模式(Direct routing) 1.N ...
- 10 Python中的代码缓存机制
目录: 1) 什么是代码块 2) 基本原理 3) 机制适用范围 4) 适用对象 5) 优势 更详细说明,参考太白老师博客 https://www.cnblogs.com/jin-xin/article ...
- BZOJ 1666 USACO 2006 Oct. 奶牛的数字游戏
直接模拟2333 #include<cstdio> #include<algorithm> using namespace std; int n,ans; void read( ...
- Spring AOP学习(六)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Leetcode 80.删除重复数组的重复项
删除重复数组的重复项 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间 ...
- 闸门机制(Gate Mechanism)
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51728107 神经网络中常说的闸门机制 ...
- 一些非常有用的工具类之javamail(from韩顺平)
之前编写一个类淘宝服务器时,需要使用javamail发送邮件,搜到的一个工具类,很有用. 需要下载导入:activation.jar和mail.jar package com.cx.service; ...
- 又见GCD
Problem Description 有三个正整数a,b,c(0<a,b,c<10^6),其中c不等于b.若a和c的最大公约数为b,现已知a和b,求满足条件的最小的c. Input ...