啊...比赛的时候输入打错了,结束之后还照着题解把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的回文的更多相关文章

  1. bzoj 1236: longpo的回文

    1236: longpo的回文 题目描述 一个字符串如果从左到右和从右到左读的结果是一样的,我们称之为回文串.现在给定一个字符串,我们有三种操作: 1.     添加一个字母在任何位置(可以在首尾添加 ...

  2. LeetCode[5] 最长的回文子串

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  3. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  4. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  5. [LeetCode] Palindrome Pairs 回文对

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  6. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  7. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  8. [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 ...

  9. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

随机推荐

  1. linux基础常用语句--新手

    查询:ls查看全部内容:ls -n删除:rm -rf 文件名创建目录: mkdir解压:rpm -验证是否安装:rpm -p 文件名rpm -ivh --nodeps 不需要前置条件的安装查询当前路径 ...

  2. 配置Struts2后运行jsp出现404的解决方法

    更新:善用控制台查看错误信息 --------------------------------------------- 原因:Java Build Path没有导入正确的jar包或者导入了但没有把相 ...

  3. CentOS7 安装、配置 Memcached

    点击访问原文地址 介绍 Memcached 是一个分布式.高性能的内存缓存系统,通过缓存内存中的数据和对象,提高和加速动态 web 应用程序的性能.它主要用于加速对数据库重度使用的站点. Memcac ...

  4. linux设置系统时间与各种阻塞

    前阵子做了一个P2P的通信系统,发现开机的时候和中间运行的时候会莫名报错,这个问题找了好久,后来从日志中看出来,所有节点上阻塞的操作同时超时. 而在超时左右,有新节点自动加入系统. 在新节点加入系统的 ...

  5. HDU - 3407 - String-Matching Automata

    先上题目: String-Matching Automata Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  6. 【UOJ34】高精度乘法(FFT)

    题意: 思路:FFT模板,自带10倍常数 type cp=record x,y:double; end; arr=..]of cp; var a,b,cur:arr; n,m,n1,n2,i,j:lo ...

  7. ci 在阿里云访问的时候404

    最近想把ci 弄到阿里云去,结果发现,本地好好的,到了阿里云就404了.网上查了下 原来是 配置文件的问题. 配置文件就是 vhost下面的 **.conf文件  要有这个才行 location / ...

  8. Display certain line(s) from a text file in Linux.

    Purpose: Display certain line or lines from a text file, such as : Display the 1000th line from file ...

  9. nmap,port扫描,获取sshserver的ip地址

    // 查看局域网的ip地址 arp - a // 同一个网段.假设用虚拟机桥接则不行 sudo nmap -sS 192.168.1.* //或者sudo nmap -sS -p 22 192.168 ...

  10. [android]Xutils具体介绍

    什么是Xutils xUtils 包括了非常多有用的android工具,xUtils 源于Afinal框架,对Afinal进行了大量重构,使得xUtils支持大文件上传,更全面的http请求协议支持, ...