# 区间上的动态规划

3
1 2 3
7
13 7 8 16 21 4 18

9
239

dp[i][j]=min(dp[i][k]+dp[k+1][j]+sum(i,j)),(i < k < j)

Accepted code:


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn=205;
int stone[maxn];
int dp[maxn][maxn];
int n;
int sum[maxn];

int main()
{
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
{
scanf("%d",&stone[i]);
}
sum[0]=0;
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+stone[i-1];

//len=1
for(int i=0;i<n;i++) dp[i][i]=0;
//len=2
for(int i=0;i<n-1;i++) dp[i][i+1]=stone[i]+stone[i+1];
//len>=3
for(int l=3;l<=n;l++)
{
for(int s=0;s+l-1<n;s++)
{
int t=s+l-1;
int temp=0x3f3f3f3f;
for(int k=s;k<t;k++)
{
temp=min(temp,dp[s][k]+dp[k+1][t]+sum[t+1]-sum[s]);
}
dp[s][t]=temp;
}
}
printf("%d\n",dp[0][n-1]);
}
}

[]是匹配的
([])[]是匹配的
((]是不匹配的
([)]是不匹配的

4
[]
([])[]
((]
([)]

0
0
3
2

《算法艺术与信息学竞赛》

1）S 形如（S′）或[S′]：

2）S 形如（S′或[S′： 先把 S′化为规则的，右边加“）”或“]”即可，则 f[i,j]= f[i+1,j]+1。
3）S 形如 S′）或 S′]： 先把 S′化为规则的，左边加“（”或“[”即可，则 f[i,j]= f[i,j-1]+1
4）把长度大于 1 的序列 SiSi+1..Sj-1Sj分为两部分: Si..Sk，Sk+1.. Sj，分别化为规则序列，则 f[i,j]=f[i,k]+f[k+1,j] ；i<=k<=j-1; 上述 4 种情况取最小值即可。

()() 0
(我就是卡上面那个数据卡了很久）

Accepted code:



#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=205;
int dp[maxn][maxn];
char s[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
int n=(int)strlen(s);
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++) dp[i][i]=1;

for(int i=n;i>=0;i--)
{
for(int j=i;j<n;j++)
{
int temp=0x3f3f3f3f;
if((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']'))
{
temp=dp[i+1][j-1];
}

for(int k=i;k<j;k++)
{
temp=min(temp,dp[i][k]+dp[k+1][j]);
}
int v1=0x3f3f3f3f,v2=0x3f3f3f3f;
if(s[j]==')'||s[j]==']')
v1=dp[i][j-1]+1;
if(s[i]=='('||s[i]=='[')
v2=dp[i+1][j]+1;
dp[i][j]=min(v1,min(v2,temp));

}
}
printf("%d\n",dp[0][n-1]);
}
}


## ﻿

Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is planning to attend as many parties as he can. Since it’s Halloween, these parties are all costume parties, Gappu always selects his costumes in such a way that it blends with his friends, that is, when he is attending the party, arranged by his comic-book-fan friends, he will go with the costume of Superman, but when the party is arranged contest-buddies, he would go with the costume of ‘Chinese Postman’.

Since he is going to attend a number of parties on the Halloween night, and wear costumes accordingly, he will be changing his costumes a number of times. So, to make things a little easier, he may put on costumes one over another (that is he may wear the uniform for the postman, over the superman costume). Before each party he can take off some of the costumes, or wear a new one. That is, if he is wearing the Postman uniform over the Superman costume, and wants to go to a party in Superman costume, he can take off the Postman uniform, or he can wear a new Superman uniform. But, keep in mind that, Gappu doesn’t like to wear dresses without cleaning them first, so, after taking off the Postman uniform, he cannot use that again in the Halloween night, if he needs the Postman costume again, he will have to use a new one. He can take off any number of costumes, and if he takes off k of the costumes, that will be the last k ones (e.g. if he wears costume A before costume B, to take off A, first he has to remove B).

Given the parties and the costumes, find the minimum number of costumes Gappu will need in the Halloween night.

Input
Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case starts with a line containing an integer N (1 ≤ N ≤ 100) denoting the number of parties. Next line contains N integers, where the ith integer ci (1 ≤ ci ≤ 100) denotes the costume he will be wearing in party i. He will attend party 1 first, then party 2, and so on.

Output
For each case, print the case number and the minimum number of required costumes.

Sample Input
2
4
1 2 1 2
7
1 2 1 1 3 2 1
Sample Output
Case 1: 3
Case 2: 4

dp[i][j]=dp[i][k]+dp[k+1][j]，其中需要costume[j]==costume[k]

dp[i][i]=1;

Accepted code:

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;
const int maxn=105;
int costume[maxn];
int dp[maxn][maxn];

int main()
{
int t;
cin>>t;
int kase=1;
while(t--)
{

int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>costume[i];

memset(dp,0,sizeof(dp));

for(int i=0;i<n;i++)
for(int j=i;j<n;j++)
dp[i][j]=j-i+1;

for(int i=n-1;i>=0;i--)
{
for(int j=i+1;j<n;j++)
{
//turn off costume
int temp=0x3f3f3f3f;
for(int k=i;k<j;k++)
if(costume[k]==costume[j])
temp=min(temp,dp[i][k]+dp[k+1][j-1]);
//dress up
dp[i][j]=min(temp,dp[i][j-1]+1);
}
}

cout<<"Case "<<kase++<<": "<<dp[0][n-1]<<endl;
}
}

We give the following inductive definition of a “regular brackets” sequence:

the empty sequence is a regular brackets sequence,
if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and
if a and b are regular brackets sequences, then ab is a regular brackets sequence.
no other sequence is a regular brackets sequence
For instance, all of the following character sequences are regular brackets sequences:

(), [], (()), ()[], ()[()]

while the following character sequences are not:

(, ], )(, ([)], ([(]

Given a brackets sequence of characters a1a2 … an, your goal is to find the length of the longest regular brackets sequence that is a subsequence of s. That is, you wish to find the largest m such that for indices i1, i2, …, im where 1 ≤ i1 < i2 < … < im ≤ n, ai1ai2 … aim is a regular brackets sequence.

Given the initial sequence ([([]])], the longest regular brackets subsequence is [([])].

Input
The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters (, ), [, and ]; each input test will have length between 1 and 100, inclusive. The end-of-file is marked by a line containing the word “end” and should not be processed.

Output
For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.

Sample Input
((()))
()()()
([]])
)[)(
([][][)
end
Sample Output
6
6
4
0
6

AC code:

#include<iostream>
#include<cstring>
using namespace std;

const int maxn=105;
int dp[maxn][maxn];

int main()
{
string s;
while(cin>>s)
{
if(s=="end") break;
memset(dp,0,sizeof(dp));

for(int i=s.size()-1;i>=0;i--)
{
for(int j=i;j<s.size();j++)
{
if((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']'))
{
dp[i][j]=dp[i+1][j-1]+2;
}
for(int k=i;k<j;k++)
{
dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]);
}

}
}
cout<<dp[0][s.size()-1]<<endl;
}
}

• 评论

• 上一篇
• 下一篇