NYoj--760(动规,LCS转LIS)

本文介绍了一种结合最长公共子序列(LCS)与最长递增子序列(LIS)的算法实现,通过优化处理将时间复杂度降低到O(nlogn),适用于处理大规模数据集的最长公共子序列问题。

2014-08-31 13:40:13

See LCS again

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
 
描述

There are A, B two sequences, the number of elements in the sequence is n、m;

Each element in the sequence are different and less than 100000.

Calculate the length of the longest common subsequence of A and B.

 
输入
The input has multicases.Each test case consists of three lines;
The first line consist two integers n, m (1 < = n, m < = 100000);
The second line with n integers, expressed sequence A;
The third line with m integers, expressed sequence B;
输出
For each set of test cases, output the length of the longest common subsequence of A and B, in a single line.
样例输入
5 4
1 2 6 5 4
1 3 5 4
样例输出
3

思路:DP好题,如果直接LCS递推求解的话,时间复杂度是O(n^2),接受不了。所以这样处理:先记录串A中每个数的位置,再在读入串B时记录串B中每个数在串A中的位置pos,然后就是在pos数组中求LIS的问题了,用经典的二分优化LIS,O(nlogn)的复杂度。
 1 /*************************************************************************
 2     > File Name: ny760.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Sun 31 Aug 2014 12:46:14 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 
16 int v1,v2;
17 int p1[100005];
18 int pos[100005];
19 int que[100005];
20 int n,m;
21 int len;
22 
23 int B_search(int val){
24     int l = 1,r = len + 1,mid;
25     while(l < r){
26         mid = l + (r - l) / 2;
27         if(que[mid] < val)
28             l = mid + 1;
29         else
30             r = mid;
31     }
32     return l;
33 }
34 
35 int main(){
36     while(scanf("%d%d",&n,&m) == 2){
37         memset(p1,0,sizeof(p1));
38         memset(pos,0,sizeof(pos));
39         for(int i = 1; i <= n; ++i){
40             scanf("%d",&v1);
41             p1[v1] = i;
42         }
43         for(int i = 1; i <= m; ++i){
44             scanf("%d",&v2);
45             pos[i] = p1[v2];
46         }
47         len = 0;
48         for(int i = 1; i <= m; ++i){
49             if(pos[i] == 0) continue;
50             int index = B_search(pos[i]);
51             que[index] = pos[i];
52             if(index > len)
53                 ++len;
54             //printf("plus:%d , len:%d\n",pos[i],len);
55         }
56         printf("%d\n",len);
57     }
58     return 0;
59 }

 

转载于:https://www.cnblogs.com/naturepengchen/articles/3947606.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值