PAT 1045 Favorite Color Stripe dfs+记忆化搜索

在这款编程挑战中,Eva的目标是从给定的颜色条纹中裁剪并重组,仅保留她最喜欢的颜色,形成最长的连续颜色条纹。通过深度优先搜索算法,程序帮助Eva找到最优解,实现其个性化颜色条纹的创造。

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 (≤10410^4104) 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

直接看代码注释吧,这种dfs的还真不太好解释。。

30分代码

#include <bits/stdc++.h>
using namespace std;

typedef pair<int,int> P;
const int maxn = 1e4+10;
int favo[205];
int color[205];
int n,nfavo,ncolor,t; 
vector<int> stripe;
map<P,int> mp;

int findStripe(int pos,int index)
{
 	// 从pos开始,搜索color[index]
	// 返回从pos开始,颜色从color[index]开始的最长的favorite color stripe的长度 
	if(index == nfavo)
		return 0;
	
	if(mp.count(make_pair(pos,index)) != 0)    //记忆化搜索 
		return mp[make_pair(pos,index)];
	
	int max_value = 0,temp = 0;
	for(int i = pos;i < stripe.size(); i++)
	{
		if(stripe[i] == color[index])
		{
			temp++;
			max_value = max(max_value,temp+findStripe(i,index+1));
		}
	}	
	if(temp == 0)               // 没有找到color[index]这种颜色 
		return findStripe(pos,index+1);
	else
		return mp[make_pair(pos,index)] = max_value;
}
int main(int argc, char const *argv[])
{
	cin >> n;
	
	cin >> nfavo;
	
	memset(favo,0,sizeof(favo));
	
	for(int i = 0;i < nfavo; i++)
	{
		scanf("%d",&t);
		color[i] = t;
		favo[t] = 1;
	}
	
	cin >> ncolor;
	for(int i = 0;i < ncolor; i++)
	{
		scanf("%d",&t);
		if(favo[t] == 1)
			stripe.push_back(t);
	}

	int longest = 0; 
	if(stripe.size() == 0)         
		cout << 0 << endl;
	else
	{
		for(int i = 0;i < nfavo; i++)          // 每一种颜色作为开头都要搜索 
			longest = max(longest,findStripe(0,i));
	} 
	cout << longest << endl;
	
	return 0;
}



这个是完整源码 python实现 Django 【python毕业设计】基于Python的天气预报(天气预测分析)(Django+sklearn机器学习+selenium爬虫)可视化系统.zip 源码+论文+sql脚本 完整版 数据库是mysql 本研究旨在开发一个基于Python的天气预报可视化系统,该系统结合了Django框架、sklearn机器学习库和Selenium爬虫技术,实现对天气数据的收集、分析和可视化。首先,我们使用Selenium爬虫技术从多个天气数据网站实时抓取气象数据,包括温度、湿度、气压、风速等多项指标。这些数据经过清洗和预处理后本研究旨在开发一个基于Python的天气预报可视化系统,该系统结合了Django框架、sklearn机器学习库和Selenium爬虫技术,实现对天气数据的收集、分析和可视化。首先,我们使用Selenium爬虫技术从多个天气数据网站实时抓取气象数据,包括温度、湿度、气压、风速等多项指标。这些数据经过清洗和预处理后,将其存储在后端数据库中,以供后续分析。 其次,采用s,将其存储在后端数据库中,以供后续分析。 其次,采用sklearn机器学习库构建预测模型,通过时间序列分析和回归方法,对未来天气情况进行预测。我们利用以往的数据训练模型,以提高预测的准确性。通过交叉验证和超参数优化等技术手段,我们优化了模型性能,确保其在实际应用中的有效性和可靠性。 最后,基于Django框架开发前端展示系统,实现天气预报的可视化。用户可以通过友好的界面查询实时天气信息和未来几天内的天气预测。系统还提供多种图表类型,包括折线图和柱状图,帮助用户直观理解天气变化趋势。 本研究的成果为天气预报领域提供了一种新的技术解决方案,不仅增强了数据获取和处理的效率,还提升了用户体验。未来,该系统能够扩展至其他气象相关的应用场景,为大众提供更加准确和及时的气象服务。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值