Medium POJ 3280 cheapest PALINDROME,DP.

Source: Internet
Author: User

Description

Keeping track of all the cows can is a tricky task so Farmer John have installed a system to automate it. He has installed on each cow an electronic ID tag that the system would read as the cows pass by a scanner. Each ID tag ' s contents is 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 is, sometimes try to spoof the system by walking backwards. While a cow whose id was "ABCBA" would read the same no matter which direction the She walks, a cow with the ID "ABCB" can Potentially register as 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 being changed by adding ' a ' at the end to form ' ABCBA ' so ' the ID is palindromic (reads the same Forwards and backwards). Some other ways to change the ID of be palindromic is include adding the three letters "BCB" to the begining to yield the ID "BCBABCB" or removing the "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 Strin G.

Unfortunately as the ID tags is electronic, each character insertion or deletion have 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 Mini Mum cost to change the ID tag so it satisfies FJ ' s requirements. An empty ID tag was considered to satisfy the requirements of reading the same forward and backward. Only letters with associated costs can is added to a string.

The problem is to make a string into a palindrome, can be arbitrarily deleted, to find the minimum cost.

Typical DP problem, for this problem can be linked to the classic topic, that is, the two string into the exact same minimum cost, this word enumerates left and right two strings, and then once to find the same minimum cost.

DP[I][J] means that the string to the left of I and J to the right of the string becomes the exact same price.

The code is as follows:

//The ━━━━━━ of gods and Beasts ━━━━━━//┏┓┏┓//┏┛┻━━━━━━━┛┻┓//┃┃//┃━┃//████━████┃//┃┃//┃┻┃//┃┃//┗━┓┏━┛//┃┃//┃┃//┃┗━━━┓//┃┣┓//┃┏┛//┗┓┓┏━━━━━┳┓┏┛//┃┫┫┃┫┫//┗┻┛┗┻┛////━━━━━━ Feel the ━━━━━━ of Meng Meng//author:whywhy//Created time:2015 July 19 Sunday 16:29 25 seconds//File name:3280.cpp#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<math.h>#include<stdlib.h>#include<time.h>using namespacestd;Const intmaxn= .;Const Long Longinf=1000000000000000LL;intn,m;CharS[MAXN];Long LongDP[MAXN][MAXN];Long LongAC[MAXN],DC[MAXN];intMain () {//freopen ("In.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout);    intb; Charts[Ten]; Long Longsum1,sum2;  while(~SCANF ("%d%d",&n,&M)) {scanf ("%s", s); memset (AC,0,sizeof(AC)); memset (DC,0,sizeof(DC));  for(intI=1; i<=n;++i) {scanf ("%s%d%d",ts,&a,&b); ac[ts[0]-'a']=A; dc[ts[0]-'a']=b; } sum1=sum2=0; dp[0][m]=0;  for(inti=n-1; j>=0;--j) {Sum1+=ac[s[j]-'a']; Sum2+=dc[s[j]-'a']; dp[0][j]=min (sum1,sum2); } sum1=sum2=0;  for(intI=1; i<=m;++i) {sum1+=ac[s[i-1]-'a']; Sum2+=dc[s[i-1]-'a']; DP[I][M]=min (sum1,sum2); }         for(intI=1; i<=m;++i) for(inti=n-1; j>=i;--j)if(s[i-1]==S[j]) dp[i][j]=dp[i-1][j+1]; ElseDp[i][j]=min (dp[i-1][j]+min (ac[s[i-1]-'a'],dc[s[i-1]-'a']), dp[i][j+1]+min (ac[s[j]-'a'],dc[s[j]-'a'])); Long Longminn=INF;  for(intI=0; i<=m;++i) Minn=min (minn,dp[i][i]);  for(intI=0; i<m;++i) Minn=min (minn,dp[i][i+1]); printf ("%lld\n", Minn); }        return 0;}
View Code

Medium POJ 3280 cheapest PALINDROME,DP.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.