nyoj 17-单调递增最长子序列 && poj 2533(动态规划,演算法)

17-单调递增最长子序列


内存限制:64MB 时间限制:3000ms Special Judge: No
accepted:21 submit:49

题目描述:

求一个字符串的最长递增子序列的长度
如:dabdbf最长递增子序列就是abdf,长度为4

输入描述:

第一行一个整数0<n<20,表示有n个字符串要处理
随后的n行,每行有一个字符串,该字符串的长度不会超过10000

输出描述:

输出字符串的最长递增子序列的长度

样例输入:

复制
3
aaa
ababc
abklmncdefg

样例输出:

1
3
7

nyoj 17 分析(动态规划):
  ①、要求整体的最大长度,我们可以从局部的最大长度来考虑;
  ②、从左到右依次考虑,每遇到一个点就从第一位开始遍历到该点,看以这个点作为前缀是否为最大值
  ③、状态方程:dp[i] = max(dp[i], d[j] + 1);

步骤:
  ①、从左到右依次遍历每一个点;
  ②、在该点基础上再从前到后通过 dp[i] = max(dp[i], d[j] + 1) 得出该点最大的值

核心代码:
1 for(int i = 0; i < n; ++ i)
2 {
3     dp[i] = 1; //初始化每个dp[MAXN];
4     for(int j = 0; j < i; ++ j)
5         if(s[j] < s[i]) dp[i] = max(dp[i], dp[j] + 1); //找出所有满足条件的s[j] ==> dp[i]最大值
6     ans = max(ans, dp[i]);
7 }

C/C++代码实现(AC):

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <queue>
 7 #include <set>
 8 #include <map>
 9 #include <stack>
10 
11 using namespace std;
12 const int MAXN = 10010;
13 
14 int main ()
15 {
16     int t;
17     scanf("%d", &t);
18     while(t --)
19     {
20         char s[MAXN];
21         scanf("%s", s);
22         int len = strlen(s), ans = -0x3f3f3f3f, dp[MAXN];
23         for(int i = 0; i < len; ++ i)
24         {
25             dp[i] = 1;
26             for(int j = 0; j < i; ++ j)
27                 if (s[j] < s[i])
28                     dp[i] = max(dp[i], dp[j] + 1);
29             ans = max(ans, dp[i]);
30         }
31         printf("%d\n", ans);
32     }
33     return 0;
34 }

 

※nyoj 17分析(演算法)【推荐】:
  ①、找出酱紫的序列:从左到右的排列是由ASCⅡ码递增;
  ②、且每一组相邻的点ASCⅡ之差最小,及就是最为接近

核心代码:
 1 cnt = 0; temp[0] = s[0];
 2 for(int i = 1; i < n; ++ i)
 3 {
 4     if(temp[cnt] < s[i]) temp[++cnt] = s[i] // cnt + 1即为所求
 5     else
 6     {
 7         for(int j = 0; j <= cnt; ++ j)
 8         {
 9             if(s[i] <= temp[j])
10             {
11                 temp[j] = s[i];
12                 break;
13             }
14         }
15     }
16 }

C/C++代码实现(AC):

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <queue>
 7 #include <set>
 8 #include <map>
 9 #include <stack>
10 
11 using namespace std;
12 const int MAXN = 10010;
13 
14 int main ()
15 {
16     int t;
17     scanf("%d", &t);
18     while(t --)
19     {
20         char s[MAXN], temp[MAXN];
21         scanf("%s", s);
22 
23         int len = strlen(s), cnt = 0;
24         temp[0] = s[0];
25         for(int i = 1; i < len; ++ i)
26         {
27             if(temp[cnt] < s[i])
28             {
29                 temp[++cnt] = s[i];
30                 continue;
31             }
32 
33             for(int j = 0; j <= cnt; ++ j)
34             {
35                 if(s[i] <= temp[j])
36                 {
37                     temp[j] = s[i];
38                     break;
39                 }
40             }
41         }
42         printf("%d\n", cnt + 1);
43     }
44     return 0;
45 }

 

Longest Ordered Subsequence
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 60426 Accepted: 27062

Description

A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence ( a1, a2, ..., aN) be any sequence ( ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).

Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.

Input

The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000

Output

Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.

Sample Input

7
1 7 3 5 9 4 8

Sample Output

4

※poj 2533 分析(演算法)【推荐】:
  ①、找出酱紫的序列:从左到右的排列是由ASCⅡ码递增;
  ②、且每一组相邻的点ASCⅡ之差最小,及就是最为接近.

 

核心代码:

  

 1 int temp[0] = A[0], cnt = 0; // cnt + 1 即为所求
 2 for(int i = 1; i < n; ++ i)
 3 {
 4     if (temp[cnt] < A[i]) temp[++cnt] = A[i];
 5     else
 6     {
 7         for(int j = 0; i <= cnt; ++ j)
 8         {
 9             if(A[i] <= temp[j])
10             {
11                 temp[j] = A[i]; // 保证序列ASCⅡ之和最小化
12                 break;
13             }
14         }
15     }
16 }

 

C/C++代码实现(AC):

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <cmath>
 6 #include <stack>
 7 #include <map>
 8 #include <queue>
 9 
10 using namespace std;
11 const int MAXN = 10010;
12 int A[MAXN], temp[MAXN];
13 
14 int main()
15 {
16     int n, cnt = 0;
17     scanf("%d", &n);
18     for(int i = 0; i < n; ++ i)
19         scanf("%d", &A[i]);
20 
21     temp[0] = A[0];
22     for(int i = 1; i < n; ++ i)
23     {
24         if(temp[cnt] < A[i]) temp[++ cnt] = A[i];
25         else
26         {
27             for(int j = 0; j <= cnt; ++ j)
28             {
29                 if(A[i] <= temp[j])
30                 {
31                     temp[j] = A[i];
32                     break;
33                 }
34             }
35         }
36     }
37     printf("%d\n", cnt + 1);
38     return 0;
39 }

 

 

转载于:https://www.cnblogs.com/GetcharZp/p/9053647.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
孪生素数是指两个素数之间的差值为2的素数对。通过筛选法可以找出给定素数范围内的所有孪生素数的组数。 在引用的代码中,使用了递归筛选法来解决孪生素数问题。该程序首先使用循环将素数的倍数标记为非素数,然后再遍历素数数组,找出相邻素数之间差值为2的素数对,并统计总数。 具体实现过程如下: 1. 定义一个数组a[N,用来标记数字是否为素数,其中N为素数范围的上限。 2. 初始化数组a,将0和1标记为非素数。 3. 输入要查询的孪生素数的个数n。 4. 循环n次,每次读入一个要查询的素数范围num。 5. 使用两层循环,外层循环从2遍历到num/2,内层循环从i的平方开始,将素数的倍数标记为非素数。 6. 再次循环遍历素数数组,找出相邻素数之间差值为2的素数对,并统计总数。 7. 输出总数。 至此,我们可以使用这个筛选法的程序来解决孪生素数问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python用递归筛选法求N以内的孪生质数(孪生素数)](https://blog.csdn.net/weixin_39734646/article/details/110990629)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [NYOJ-26 孪生素数问题](https://blog.csdn.net/memoryofyck/article/details/52059096)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值