1. 程式人生 > >PAT 1045 Favorite Color Stripe (30) 分動態規劃 最長字首問題 燚

PAT 1045 Favorite Color Stripe (30) 分動態規劃 最長字首問題 燚

Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form her favorite color stripe.

It is said that a normal human eye can distinguish about less than 200 different colors, so Eva's favorite colors are limited. However the original stripe could be very long, and Eva would like to have the remaining favorite stripe with the maximum length. So she needs your help to find her the best result.

Note that the solution might not be unique, but you only have to tell her the maximum length. For example, given a stripe of colors {2 2 4 1 5 5 6 3 1 1 5 6}. If Eva's favorite colors are given in her favorite order as {2 3 1 5 6}, then she has 4 possible best solutions {2 2 1 1 1 5 6}, {2 2 1 5 5 5 6}, {2 2 1 5 5 6 6}, and {2 2 3 1 1 5 6}.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤200) which is the total number of colors involved (and hence the colors are numbered from 1 to N). Then the next line starts with a positive integer M (≤200) followed by M Eva's favorite color numbers given in her favorite order. Finally the third line starts with a positive integer L (≤10​4​​) which is the length of the given stripe, followed by L colors on the stripe. All the numbers in a line a separated by a space.

Output Specification:

For each test case, simply print in a line the maximum length of Eva's favorite stripe.

Sample Input:

6
5 2 3 1 5 6
12 2 2 4 1 5 5 6 3 1 1 5 6

Sample Output:

7

題目大意:用正整數表示顏色,第一行給定顏色範圍上限N,第二行:給定Each喜歡的顏色個數M 緊接著給出M個他喜歡的顏色數字。第三行:給出總的顏色個數L 緊接著給出L個顏色。要求:從L個顏色中篩選出Each喜歡的最長顏色串,且顏色的相對次序和他喜歡的顏色的相對順序一致,不要求包含他所有喜歡的顏色。

   思路:1.讀完題的第一感覺為  動態規劃,因為每一個顏色的選定都依賴於前面求出的最大字串,具有最優子結構性,不同的選擇方案間又有重疊  即 子問題重疊性,這是動態規劃的基本性質

              2.明確演算法後 就開始設計。要求最長字首序列,於是每一次的更新就必須在前一次的基礎上進行,動態規劃是用空間換時間的方法,由於該題只求最長字首的長度,不要求回溯。所以就只開闢一個n+1的陣列count。(如果此題要求找出所有最長字首的情況,則需要開闢L*M的二維陣列,以便最後回溯尋找所有情況)。

              3.具體演算法:1.將Each喜歡的顏色存在loveColor的陣列中,總的顏色存在allColor的陣列中。

                                   2.依次取出loverColor中的元素loverColor[i]與allColor中的元allColor[j]素逐個比較,如果                                                                      allColor[j]==loverColor[i],則在計數陣列count上執行操作count[j+1]+=1(表示allColor[j+1]的最長字首數                                         目),如果allColor[j]!=loverColor[i],則比較count[j]與count[j+1]的大小,如果count[j+1]<count[j]則表                                         示有比現在更長的字首,因此更新count[j+1]=count[j].

                                  3.重複2操作直至loveColor末尾,最後count[n]上的數即為最長字首數量。

難點:理解動態規劃過程。

#include<iostream>
#include<vector>
using namespace std;
int G[10001];
void input(vector<int>&loveColor,vector<int>&allColor){
	int n;
	cin>>n;
	int m;
	cin>>m;
    //輸入並判斷loveColor是否在範圍內
	for(int i=0;i<m;i++){
		int temp;
		cin>>temp;
		if(temp>=1&&temp<=n)
			loveColor.push_back(temp);
	}
	cin>>m;
    //輸入並判斷allColor是否在範圍內
	for(int i=0;i<m;i++){
		int temp;
		cin>>temp;
		if(temp>=1&&temp<=n)
			allColor.push_back(temp);
	}
}
//計數並求出最長字首
void computeTheMax(vector<int>&loveColor,vector<int>&allColor){
	for(int i=0;i<loveColor.size();i++){
		int j=0;
        //從左到右逐個檢測allColor[j]是否等於loveColor[i]
		while(allColor[j]!=loveColor[i]&&j<allColor.size())
			j++;
		G[j+1]+=1;//在j的下一位加以,表示G[j+1]前的最長字首數
		j++;
        //判斷並更新後面的字首數目
		for(;j<allColor.size();j++){
			if(allColor[j]==loveColor[i])
			G[j+1]=G[j]+1;
		else if(G[j]>G[j+1])
			G[j+1]=G[j];
		}
	}
}
int main(){
	vector<int>loveColor;
	vector<int>allColor;
	input(loveColor,allColor);
	computeTheMax(loveColor,allColor);
	cout<<G[allColor.size()]<<endl;
}