zoj 2672 Fibonacci Subsequence(hash + dp)
来源:互联网 发布:虚拟软件下载 编辑:程序博客网 时间:2024/05/23 01:14
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1672
A sequence of integer numbers a1 , a2 , ..., an is called a Fibonacci sequence if ai = ai-2+ai-1 for all i=3,4,...,n.
Given a sequence of integer numbers c1 , c2 , ..., cm you have to find its longest Fibonacci subsequence.
Input
There are several test cases in the input. The first line of each case contains m (1 <= m <= 3,000). Next line contains m integer numbers not exceeding 109 by their absolute value.There is a new line between each case.
Output
On the first line of each case print the maximal length of the Fibonacci subsequence of the given sequence. On the second line print the subsequence itself.There is a new line between each case.
Example
InputOutput101 1 3 -1 2 0 5 -1 -1 8
51 -1 0 -1 -1
#include <cstdio>#include <cstring>#include <iostream>#include <hash_map>using namespace std;using __gnu_cxx::hash_map;const int maxn=3010;hash_map<int,int> mymap;hash_map<int ,int >::iterator ix;void show(){ for(ix=mymap.begin();ix!=mymap.end();ix++){ cout<<ix->first<<" "<<ix->second<<" "; } cout<<endl;}int a[maxn];//int dp[maxn][maxn];short dp[maxn][maxn];int main(){ //freopen("cin.txt","r",stdin); int n; bool first=1; while(cin>>n){ if(!first)puts(""); first=0; for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++)dp[i][j]=1; } mymap.clear(); int top=0,low=a[1],high; for(int i=n;i>=1;i--){ for(int j=1;j<i;j++){ int k=a[i]+a[j]; ix=mymap.find(k); if(ix!=mymap.end()){ dp[j][i]=dp[i][ix->second]+1; // 小下标 --- 大下标 if(dp[j][i]>top){ top=dp[j][i]; low=j; high=i; } } } mymap[a[i]]=i; } if(n==1){ printf("1\n%d\n",a[1]); continue; } if(n==2) { printf("2\n%d %d\n",a[1],a[2]); continue; } if(top==0){ printf("2\n%d %d\n",a[1],a[2]); continue; } printf("%d\n",top+1); printf("%d",a[low]); int q1=a[low],q2=a[high]; for(int i=0;i<top;i++){ // when top=4 sum of numbers is 5 printf(" %d",q2); int t=q1; q1=q2; q2=t+q2; } puts(""); } return 0;}又犯了逻辑错误,主函数的结果处理如果是这样是WA的:
if(n==1){ printf("1\n%d\n",a[1]); } else if(n==2) { printf("2\n%d %d\n",a[1],a[2]); continue; } if(top==0){ printf("2\n%d %d\n",a[1],a[2]); } else { printf("%d\n",top+1); printf("%d",a[low]); int q1=a[low],q2=a[high]; for(int i=0;i<top;i++){ // when top=4 sum of numbers is 5 printf(" %d",q2); int t=q1; q1=q2; q2=t+q2; } puts(""); }
0 0
- zoj 2672 Fibonacci Subsequence(hash + dp)
- ZOJ:2672 Fibonacci Subsequence(动态规划+hash)
- zoj 2672 DP+hash
- zoj 1733 Common Subsequence dp
- ZOJ 1733 Common Subsequence【DP】
- ZOJ 2136 Longest Ordered Subsequence 【DP】
- ZOJ 1136 Longest Ordered Subsequence DP
- [ZOJ 3817 Chinese Knot] 字符串hash+DP
- zoj 2136 Longest Ordered Subsequence(DP经典~)
- zoj 1733 || hdu 1159 Common Subsequence(Easy , classics DP)
- ZOJ 3349 Special Subsequence(DP+线段树优化)
- Zoj 2432 Greatest Common Increasing Subsequence(DP)
- ZOJ 3349 Special Subsequence(线段树优化DP)
- zoj 3349 Special Subsequence(dp+线段树优化)
- ZOJ 3349 Special Subsequence(DP+线段树优化)
- ZOJ 3349 Special Subsequence【dp+线段树优化】
- ZOJ 2136 Longest Ordered Subsequence(简单DP)
- zoj2672 Fibonacci Subsequence
- C语言 学生成绩管理系统
- 关于 Windows 8.1 虚拟光驱问题
- 刀哥多线程串行队列gcd-04-dispatch_queue_serial
- C++基础---位操作符
- 数据结构 学习之图
- zoj 2672 Fibonacci Subsequence(hash + dp)
- GYM 100285 I. The old Padawan(二分+简单模拟)
- ListView中显示bitmap
- Android Studio系列教程二--基本设置与运行
- 输入n,分别用*输出边长为n的实心菱形和空心菱形。
- 刀哥多线程之并发队列gcd-05-dispatch_queue_concurrent
- linux下cp,tar相关权限问题,-p参数
- android解决内存溢出的问题
- 刀哥多线程之主队列gcd-06-main_queue