hdu6078 Wavel Sequence(分析优化递推过程,好题)

题意:

序列a有n个数,序列b有m个数。问,两个序列有多少个公共子序列满足“波浪状”。“波浪状”数组形如 a1 < a2 > a3 < a4 > a5 ……

1 <= n,m <= 2000

分析:

看了一个博客分析的非常好
http://blog.csdn.net/weyoungg/article/details/76735843

代码就是按照这个博主的思路,只不过用滚动数组把第一维滚掉了。

代码:

#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
#define ms(a,b) memset(a,b,sizeof(a))
typedef long long ll;
const int MAXN = 2005;
const double EPS = 1e-8;
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
int n, m, a[MAXN], b[MAXN];
ll f[MAXN][2];

int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++)    scanf("%d", &a[i]);
        for (int i = 1; i <= m; i++)    scanf("%d", &b[i]);
        ms(f, 0);
        ll ans = 0;
        //f[][1] 波峰 f[][0] 波谷
        for (int i = 1; i <= n; i++) {
            ll top = 1, btm = 0;
            for (int j = 1; j <= m; j++) {
                if (a[i] == b[j]) {
                    f[j][0] += top;
                    f[j][1] += btm;
                    ans = (ans + top + btm) % MOD;
                } else if (a[i] > b[j]) {
                    (btm += f[j][0]) %= MOD;
                } else {
                    (top += f[j][1]) %= MOD;
                }
            }
        }
        printf("%lld\n",ans);
    }

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值