首页 > 其他分享 >P2890 [USACO07OPEN]Cheapest Palindrome G

P2890 [USACO07OPEN]Cheapest Palindrome G

时间:2022-09-29 12:00:30浏览次数:56  
标签:Palindrome Cheapest int 2222 char P2890

#include<bits/stdc++.h>
using namespace std;
class solve{
	public:
		int n,m;
		map < char , int > Map,Maps;
		char s[2222];
		int f[2222][2222];
		void main(){
			cin>>n>>m;
			scanf("%s",s+1);
			memset(f,0x3f3f3f3f,sizeof(f));
			for(int i=1; i<=n; i++)
			{
				char ch[1];
				int x,y;
				scanf("%s ",ch);
				scanf("%d%d",&x,&y);Map[ch[0]]=x;Maps[ch[0]]=y;
			}
			for(int i=1; i<=m; i++)f[i][i]=0;
			for(int len=1; len<m; len++)
			{
				for(int i=1; i+len<=m; i++)
				{
					int j=i+len;
					f[i][j]=min(f[i][j-1]+Maps[s[j]],f[i][j]);
					f[i][j]=min(f[i+1][j]+Maps[s[i]],f[i][j]);
					f[i][j]=min(f[i][j-1]+Map[s[j]],f[i][j]);
					f[i][j]=min(f[i+1][j]+Map[s[i]],f[i][j]);
					if(s[i]==s[j])
					{
						if(i==j-1)
						f[i][j]=0;
						else
						f[i][j]=min(f[i][j],f[i+1][j-1]);
					}
				}
			}
			cout<<f[1][m]<<endl;
		}
}x;
int main()
{
	x.main();
}

标签:Palindrome,Cheapest,int,2222,char,P2890
From: https://www.cnblogs.com/dadidididi/p/16740970.html

相关文章

  • lc234判断回文链表 isPalindrome python3
    给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。classSolution:defisPalindrome(self,head:ListNode)->bool:v......
  • leetcode 409 Longest Palindrome 最长回文串(简单)
    一、题目大意给定一个包含大写字母和小写字母的字符串s,返回通过这些字母构造成的最长的回文串。在构造过程中,请注意区分大小写。比如"Aa"不能当做一个回文字符......
  • LeetCode 131 Palindrome Partitioning
    Givenastrings,partitionssuchthateverysubstringofthepartitionisapalindrome.Returnallpossiblepalindromepartitioningofs.Apalindromestring......