eoj2083 DP

ZigZag

Time Limit:1000MSMemory Limit:30000KB
Total Submit:257Accepted:106

Description

A sequence of numbers is called a zig-zag sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a zig-zag sequence.
For example, 1,7,4,9,2,5 is a zig-zag sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, 1,4,7,2,5 and 1,7,4,5,5 are not zig-zag sequences, the first because its first two differences are positive and the second because its last difference is zero.


-sequence contains between 1 and 50 elements, inclusive.
-Each element of sequence is between 1 and 1000, inclusive.

Input

Given a sequence of integers, sequence, return the length of the longest subsequence of sequence that is a zig-zag sequence. A subsequence is obtained by deleting some number of elements (possibly zero) from the original sequence, leaving the remaining elements in their original order.

Output

output the length of the longest subsequence of sequence that is a zig-zag sequence.

Sample Input

6
1 7 4 9 2 5

/*
10
1 17 5 10 13 15 10 5 16 8
*/

Sample Output

6

/*
7
*/


题目:EOJ2083

 

题目分析:思路类似求最长上升子序列。需要注意的是是否满足zag—tag还需考察一个点的前一个zag_tag串的值,故应保存每个节点的直接前驱节点的。用dp[i].val表示以i节点结尾的zag_tag串的最大值,用dp[i].pre表示i节点在zag_tag串中前一个节点的值,若i为zag_tag串的首节点则置dp[i].pre=-1.则有:对点i,令j为0至i-1;1.若dp[j].pre=-1,则只需考察i和j节点值是否相等,更新dp[i]的值,2.若dp[j].pre不等以-1,则比较a[j]-a[dp[j].pre])*(a[j]-a[i])和0的大小,可更新dp[i]的值。最后遍历dp可得最大值。

 

AC代码:

#include <iostream>

#include <cstring>

 

using namespace std;

 

struct Node

{

   int val,pre;

}dp[55];

 

int main()

{

   int n,a[55]={},i,j;

   memset(dp,0,sizeof(dp));

   for(i=0;i<55;++i)

       dp[i].pre=-1;

   cin>>n;

   for(i=0;i<n;++i)

       cin>>a[i];

   if(n==1)

       cout<<"1"<<endl;

   else if(n==2)

    {

       if(a[1]!=a[0])

           cout<<"2"<<endl;

       else

           cout<<"1"<<endl;

    }

   else

    {

       dp[0].val=1;

       dp[0].pre=-1;

       for(i=1;i<n;++i)

       {

           for(j=0;j<i;++j)

           {

                if(dp[j].pre==-1)

               {

                    if(a[i]!=a[j])

                    {

                       if(dp[i].val<=dp[j].val)

                        {

                           dp[i].val=dp[j].val+1;

                            dp[i].pre=j;

                        }

                    }

                }

                else

                {

                    if((a[j]-a[dp[j].pre])*(a[j]-a[i])>0 )

                    {

                       if(dp[i].val<=dp[j].val)

                        {

                           dp[i].val=dp[j].val+1;

                            dp[i].pre=j;

                        }

                    }

                }

           }

       }

       int ans=0;

       for(i=0;i<n;++i)

           ans=max(ans,dp[i].val);

       cout<<ans<<endl;

    }

   return 0;

}



### 关于EOJ DNA排序问题的解题思路 在处理EOJ中的DNA排序问题时,主要挑战在于如何高效地完成字符串数组的排序以及去重操作。由于题目涉及两个测试点可能因时间复杂度较高而超时,因此需要优化算法设计。 #### 数据结构的选择 为了降低时间复杂度并提高效率,可以引入`std::map`或者`unordered_map`来辅助实现去重功能[^1]。这些数据结构能够快速判断某项是否存在集合中,并支持高效的插入和查找操作。具体来说: - 使用 `std::set` 可以自动去除重复元素并对结果进行升序排列; - 如果还需要自定义比较逻辑,则可以选择基于哈希表的数据结构如 `unordered_set` 配合手动排序。 #### 排序策略 对于给定的一组DNA序列(通常表示为长度固定的字符串),按照字典顺序对其进行排序是一个常见需求。C++标准库提供了非常方便的方法来进行此类任务——即利用 `sort()` 函数配合合适的比较器函数对象或 lambda 表达式来指定所需的排序规则。 下面展示了一个简单的例子用于说明如何读取输入、执行必要的预处理步骤(包括但不限于删除冗余条目),最后输出经过整理的结果列表: ```cpp #include <bits/stdc++.h> using namespace std; int main(){ set<string> uniqueDNAs; string line, dna; while(getline(cin,line)){ stringstream ss(line); while(ss>>dna){ uniqueDNAs.insert(dna); // 自动过滤掉重复项 } } vector<string> sortedUnique(uniqueDNAs.begin(),uniqueDNAs.end()); sort(sortedUnique.begin(),sortedUnique.end()); for(auto it=sortedUnique.cbegin();it!=sortedUnique.cend();++it){ cout<<*it; if(next(it)!=sortedUnique.cend())cout<<" "; } } ``` 上述程序片段实现了基本的功能模块:从标准输入流逐行解析得到各个独立的DNA片段;借助 STL 容器特性轻松达成无重复记录维护目的;最终依据字母大小关系重新安排各成员位置后再统一打印出来[^3]。 #### 学习延伸至自然语言处理领域 值得注意的是,在计算机科学特别是机器学习方向上,“上下文”概念同样重要。例如 Word2Vec 这样的技术就是通过考察周围词语环境来捕捉特定词汇的意义特征[^2]。尽管两者应用场景差异显著,但从原理层面看均体现了对局部模式挖掘的关注。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值