当前位置:网站首页>2022杭电多校第三场
2022杭电多校第三场
2022-07-29 13:55:00 【fslse】
f[i][0]表示序列S前i个元素已完成匹配且第i个元素来自序列P的方案数
f[i][1]表示序列S前i个元素已完成匹配且第i个元素来自序列Q的方案数
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0),cin.tie(0)
#define endl '\n'
typedef long long ll;
const ll mod = 998244353;
const int N = 2e6 + 10;
int n;
int a[N], b[N], c[N], cnt[N], pos[N][2];
ll f[N][2];
void solve(){
cin >> n;
for (int i = 1;i <= n;++i)cin >> a[i], pos[a[i]][0] = i;
for (int i = 1;i <= n;++i)cin >> b[i], pos[b[i]][1] = i;
memset(cnt + 1, 0, n * 4);
for (int i = 1;i <= n * 2;++i)cin >> c[i], ++cnt[c[i]];
if (*max_element(cnt + 1, cnt + 1 + n) > 2){
cout << 0 << endl;
return;
}
for (int i = 1;i <= n * 2;++i)
f[i][0] = f[i][1] = 0;
f[1][!(a[1] == c[1])] = 1, f[1][b[1] == c[1]] = 1;
for (int i = 2, j;i <= n * 2;++i){
if (pos[c[i - 1]][0] + 1 == pos[c[i]][0])f[i][0] = (f[i][0] + f[i - 1][0]) % mod;
if (pos[c[i - 1]][1] + 1 == pos[c[i]][1])f[i][1] = (f[i][1] + f[i - 1][1]) % mod;
j = i - pos[c[i - 1]][0];
if (b[j] == c[i])f[i][1] = (f[i][1] + f[i - 1][0]) % mod;
j = i - pos[c[i - 1]][1];
if (a[j] == c[i])f[i][0] = (f[i][0] + f[i - 1][1]) % mod;
}
cout << (f[n * 2][0] + f[n * 2][1]) % mod << endl;
}
int main(){
IOS;
int T;
cin >> T;
while (T--){
solve();
}
return 0;
}
边栏推荐
- 通过二维顺序表实现杨辉三角
- 一文搞懂JS的原型链
- 【LeetCode】593. 有效的正方形
- C#实现线程管理类
- 【论文阅读】Anomaly Detection in Video via Self-Supervised and Multi-Task Learning
- PHP代码审计得这样由浅入深地学
- 2022年了!还在用定时器实现动画?赶紧试试requestAnimationFrame吧!
- 【FreeSwitch开发实践】自定义模块创建与使用
- What is the difference between the legendary server GOM engine and the GEE engine?
- 何为擦除机制,泛型的上界?
猜你喜欢
随机推荐
线上支付,出款和收款
力扣541. 反转字符串 II ----双指针解法
通过二维顺序表实现杨辉三角
R Error in :missing values are not allowed in subscripted assignments of data frames
PHP代码审计得这样由浅入深地学
题目 1125: C语言训练-委派任务*
十种实现延迟任务的方案
EA&UML日拱一卒-活动图::Variable Actions(续)
即时通讯移动端开发之网络连接优化
480-82(59、151)
grid的使用
【10点公开课】:快手GPU/FPGA/ASIC异构平台的应用探索
威纶通触摸屏制作自定义欢迎界面的几种方法介绍
工作效率-十五分钟让你快速学习Markdown语法到精通排版实践备忘
How to set the explosion rate of legendary humanoid?Humanoid increase tutorial
The Location object of BOM series
上线前配置
分布式事务方案
国内helm快速安装和添加常用charts仓库
已解决SyntaxError: invalid character in identifier









