## Siukwan Program

http://siukwan.sinaapp.com/

# 1045. Favorite Color Stripe (30)

1.这个和最长公共子序列问题相类似（LCS）

2.不同的地方是允许元素重复，如｛a｝和｛aaa｝，匹配出来的是3，a可以重复3次

3.该问题一开始卡在了输入格式上

4.动态规划方程：dp[i][j]表示f[0~i]与o[0~j]匹配的最大长度

5.还有一种最简单的方法:dp[i][j]=max(dp[i-1][j],dp[i][j-1],dp[i-1][j-1]),如果f[i]==o[j]，则dp[i][j]++：（采用了LCS的思想）

//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;
/*
6
5 2 3 1 5 6
12 2 2 4 1 5 5 6 3 1 1 5 6

6
5 2 3 1 5 6
10 2 2 3 1 3 1 3 1 3 1

6
5 2 3 1 5 6
10
5 6 6 6 6 6 6 6 6 6 6

6
5 2 3 1 5 6
10
2 6 6 6 6 6 6 6 6 6 6

6
5 2 3 1 5 6
10
2 2 2 6 6 6 6 6 6 6 6

6
5 2 3 1 5 6
10
2 2 2 6 6 6 6 5 6 6 6

6
5 2 3 1 5 6
10
2 2 2 6 6 6 6 6 5 6 6
*/
int main(void)
{
int colourSum,fnum, onum;
cin >> colourSum>> fnum;
vector<int> f(fnum);
for (int i = 0; i<fnum; i++)
{
scanf("%d", &f[i]);
}
cin >> onum;
vector<int> o(onum);

for (int i = 0; i<onum; i++)
{
scanf("%d", &o[i]);
}

vector<vector<int>> dp(fnum+1, vector<int>(onum+1,0));

int thisMax = 0;
for (int i = 1; i<=f.size(); i++)
{
for (int j = 1; j<=o.size(); j++)
{
dp[i][j] = max(max(dp[i - 1][j], dp[i][j - 1]), dp[i-1][j-1]);
if (f[i-1] == o[j-1])
dp[i][j]++;
}
}
cout << dp[fnum][onum] << endl;
return 0;
}


//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;
/*
6
5
2 3 1 5 6
12 2 2 4 1 5 5 6 3 1 1 5 6

4
5
2 3 1 5 6
10 2 2 3 1 3 1 3 1 3 1

2
5
2 3 1 5 6
10
5 6 6 6 6 6 6 6 6 6 6

2
5
2 3 1 5 6
10
2 6 6 6 6 6 6 6 6 6 6

2
5
2 3 1 5 6
10
2 2 2 6 6 6 6 6 6 6 6
*/
int main(void)
{
int colorSum,fnum, onum;
cin >> colorSum>> fnum;
vector<int> f(fnum);
for (int i = 0; i<fnum; i++)
{//输入喜欢的颜色
scanf("%d", &f[i]);
}
cin >> onum;
vector<int> o(onum);

for (int i = 0; i<onum; i++)
{//输入源材料
scanf("%d", &o[i]);
}

vector<vector<int>> dp(fnum, vector<int>(onum));
for (int i = 0; i<f.size(); i++)
{//初始化边界数组
if (f[i] == o[0])
dp[i][0] = 1;
else if (i == 0)
dp[i][0] = 0;
else
dp[i][0] = dp[i - 1][0];
}

for (int i = 0; i<f.size(); i++)
{
for (int j = 1; j<o.size(); j++)
{
if (f[i] == o[j])
{//如果相等，那么dp[i][j]由dp[i][j-1]和dp[i-1][j-1]的最大值构成
if (i != 0)
dp[i][j] = max(dp[i][j - 1] + 1, dp[i - 1][j - 1] + 1);
else
dp[i][j] = dp[i][j - 1] + 1;
}
else
{
if (i != 0)
dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);//取包含上一个f的长度和不包含上一个f的长度两者最大值
else
dp[i][j] = dp[i][j - 1];
}
}
}
cout << dp[fnum - 1][onum - 1] << endl;
return 0;
}

#### 1045. Favorite Color Stripe (30) -LCS允许元素重复

2015-07-16 13:10:03

#### 【PAT 1045】 Favorite Color Stripe 最长公共子序列LCS

2014-02-25 16:51:48

#### PAT 1045. Favorite Color Stripe (30) 变种LCS或LIS

2017-08-29 18:40:16

2014-05-12 23:11:01

#### 【PAT】1045. Favorite Color Stripe (30)【贪心算法】

2018-04-15 18:39:53

#### 1045. Favorite Color Stripe (30) 简单动态规划(LCS的变形)

2015-12-02 19:44:19

#### 1045. Favorite Color Stripe (30)

2016-02-25 16:22:50

#### 1045. Favorite Color Stripe (30)(LISDP)

2018-02-28 20:29:26

#### 1045. Favorite Color Stripe

2014-07-09 21:12:16

#### PAT 1045. Favorite Color Stripe (30)

2014-11-17 09:56:54