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. ...
随机推荐
- Python学习笔记之map、zip和filter函数
这篇文章主要介绍 Python 中几个常用的内置函数,用好这几个函数可以让自己的代码更加 Pythonnic 哦 1.map map() 将函数 func 作用于序列 seq 的每一个元素,并返回处理 ...
- Django-利用paginator模块实现分页
Django专门提供了一个paginator模块,实现分页很easy. 下面的例子引用了django官方文档:https://docs.djangoproject.com/en/1.11/topics ...
- 【15】AngularJS 输入验证
AngularJS 输入验证 AngularJS 表单和控件可以验证输入的数据. 输入验证 AngularJS 表单和控件可以提供验证功能,并对用户输入的非法数据进行警告. 客户端的验证不能确保用 ...
- js cookies all in one
js cookies all in one cookies // http://10.1.5.202/auto-deploy-platform/publish/index.html // 非当前 UR ...
- [luoguP1854] 花店橱窗布置(DP)
传送门 f[i][j] 表示前 i 盆花,放到前 j 个花盆中的最优解 pre[i][j] 记录前驱 代码 #include <cstdio> #include <cstring&g ...
- noip模拟赛 蒜头君的树
分析:这道题问的是树上整体的答案,当然要从整体上去考虑. 一条边对答案的贡献是这条边一端连接的点的个数*另一端连接的点的个数*边权,可以用一次dfs来统计答案,之后每次更改操作在原答案的基础上增减就好 ...
- BAT经典面试题,深入理解Java内存模型JMM
Java 内存模型 Java 内存模型(JMM)是一种抽象的概念,并不真实存在,它描述了一组规则或规范,通过这组规范定义了程序中各个变量(包括实例字段.静态字段和构成数组对象的元素)的访问方式.试图屏 ...
- 14、Java并发性和多线程-Java ThreadLocal
以下内容转自http://ifeve.com/java-theadlocal/: Java中的ThreadLocal类可以让你创建的变量只被同一个线程进行读和写操作.因此,尽管有两个线程同时执行一段相 ...
- vector实现
typedef int size_type; /* vector维护的是一个连续线性空间,提供的迭代器是Random Access Iterators即普通指针 */ template <cla ...
- zoj 1671 Walking Ant
Walking Ant Time Limit: 2 Seconds Memory Limit: 65536 KB Ants are quite diligent. They sometime ...