ACM最长单调递增子序列问题(动态规划)o(n*n)C++实现
2011-11-03 14:57
435 查看
最长单调递增子序列问题(动态规划)o(
)
// 最长单调递增子序列.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#define N 100
using namespace std;
//递归打印最长递增子序列
void print_it(char *str,int *pre,int last)
{
if(last==-1)
return;
else
{
print_it(str,pre,pre[last]);
cout<<str[last];
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//存储原字符串
char str
;
//len[j]存储到str[j]为止的最长递增子序列长度
int len
;
//pre[j]存储到str[j]为止的最长递增子序列的前一个字符的位置
int pre
;
int cases;
cout<<"请输入案例个数:"<<endl;
cin>>cases;
while(cases--)
{
cout<<"请输入字符串:"<<endl;
cin>>str;
//初始化各变量
int i;
int length = strlen(str);
for(i=0;i<length;i++)
{
len[i] = 1;
pre[i] = -1;
}
int j;
int max = 0;
for(j=1;j<length;j++)
{
for(i=0;i<j;i++)
{
max = 0;
if(str[j]>str[i])
{
max = len[i]+1;
if(max>len[j])
{
len[j] = max;
pre[j] = i;
}
}
}
}
//找最大的len[i]
//last为最长的递增子序列的最后一个字符的位置
max = 0;
int last;
for(i=0;i<length;i++)
{
if(len[i]>max)
{
max = len[i];
last = i;
}
}
cout<<"最长递增子序列的长度为:"<<max<<endl;
cout<<"最长递增子序列为:"<<endl;
print_it(str,pre,last);
cout<<endl;
}
system("pause");
return 0;
}
-----------------------------------------------程序测试----------------------------------------
请输入案例个数:
2
请输入字符串:
abceabf
最长递增子序列的长度为:5
最长递增子序列为:
abcef
请输入字符串:
abcabcdef
最长递增子序列的长度为:6
最长递增子序列为:
abcdef
请按任意键继续. . .
)
// 最长单调递增子序列.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#define N 100
using namespace std;
//递归打印最长递增子序列
void print_it(char *str,int *pre,int last)
{
if(last==-1)
return;
else
{
print_it(str,pre,pre[last]);
cout<<str[last];
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//存储原字符串
char str
;
//len[j]存储到str[j]为止的最长递增子序列长度
int len
;
//pre[j]存储到str[j]为止的最长递增子序列的前一个字符的位置
int pre
;
int cases;
cout<<"请输入案例个数:"<<endl;
cin>>cases;
while(cases--)
{
cout<<"请输入字符串:"<<endl;
cin>>str;
//初始化各变量
int i;
int length = strlen(str);
for(i=0;i<length;i++)
{
len[i] = 1;
pre[i] = -1;
}
int j;
int max = 0;
for(j=1;j<length;j++)
{
for(i=0;i<j;i++)
{
max = 0;
if(str[j]>str[i])
{
max = len[i]+1;
if(max>len[j])
{
len[j] = max;
pre[j] = i;
}
}
}
}
//找最大的len[i]
//last为最长的递增子序列的最后一个字符的位置
max = 0;
int last;
for(i=0;i<length;i++)
{
if(len[i]>max)
{
max = len[i];
last = i;
}
}
cout<<"最长递增子序列的长度为:"<<max<<endl;
cout<<"最长递增子序列为:"<<endl;
print_it(str,pre,last);
cout<<endl;
}
system("pause");
return 0;
}
-----------------------------------------------程序测试----------------------------------------
请输入案例个数:
2
请输入字符串:
abceabf
最长递增子序列的长度为:5
最长递增子序列为:
abcef
请输入字符串:
abcabcdef
最长递增子序列的长度为:6
最长递增子序列为:
abcdef
请按任意键继续. . .
相关文章推荐
- ACM最长单调递增子序列问题(动态规划)o(n*n)C++实现
- 算法导论-第15章-动态规划:钢条切割问题自底向下方法C++实现
- 0-1背包问题与完全背包问题C++实现 动态规划
- 动态规划的简要总结和四个经典问题的c++实现
- ACM最长公共子序列问题(动态规划)C++实现
- 0-1背包问题与完全背包问题C++实现 动态规划
- 0-1背包问题与完全背包问题C++实现 动态规划
- c++实现0-1背包问题完整源码(动态规划实现)
- 一个无聊男人的疯狂《数据结构与算法分析-C++描述》学习笔记 用C++/lua/python/bash的四重实现(3) 最大子序列和问题
- DNA序列问题 c++实现
- 0/1背包问题 - 动态规划(C++实现)
- ACM最长公共子序列问题(动态规划)C++实现
- 动态规划(背包问题,最长递增子序列,硬币问题)java实现
- c++实现0-1背包问题完整源码(动态规划实现)
- 动态规划 数塔问题求解 C++实现
- 最大子序列和问题的解——C++实现;
- 数据结构迷宫问题C++实现
- [动态规划]最长不降子序列问题-N*N算法
- [UE4]C++实现动态加载的问题:LoadClass<T>()和LoadObject<T>()
- [转贴]从零开始学C++之STL(二):实现一个简单容器模板类Vec(模仿VC6.0 中 vector 的实现、vector 的容量capacity 增长问题)