当前位置:网站首页>【codeforces round#800 B. Paranoid String】DP
【codeforces round#800 B. Paranoid String】DP
2022-07-17 22:57:00 【宇智波一打七~】
题目链接
题意:
一个"10"能被"0"替换,一个"01"能被"1"替换,给你一个长度为n的01串,问有多少子串能通过变换成为长度为1的串
分析:
首先想到的就是DP了,f[i][0]表示的是以第i个元素结尾的能变换成"0"这个字母的所有方案数,f[i][1]表示的是以第i个元素结尾的能变换成"1"这个字母的所有方案数,那么当第i个元素是’0’时,f[i][0]能从f[i-1][1]来转移过来,而且还要+1,因为它本身就算是一个,这样子得出的结果是少的,因为000这种和111这种没算进去,f[i][0]的更新110这种也要算进去的,那么就把fi][2]表示成以第i个元素结尾,能形成00这种结尾的方案数,把f[i][3]表示成能形成11这种结尾的方案数,具体请看代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 200010;
char s[N];
long long f[N][4];
void solve(){
int n;
scanf("%d%s",&n,s+1);
long long ans = 0;
for(int i=1;i<=n;i++){
if(s[i] == '0'){
f[i][0] = f[i-1][1] + 1 + f[i-1][3];
f[i][1] = 0;
f[i][2] = f[i-1][0] + f[i-1][2];
f[i][3] = 0;
}
else{
f[i][0] = 0;
f[i][1] = f[i-1][0] + 1 + f[i-1][2];
f[i][2] = 0;
f[i][3] = f[i-1][1] + f[i-1][3];
}
ans += f[i][0] + f[i][1];
}
cout<<ans<<endl;
}
int main(){
int _;
scanf("%d",&_);
while(_--) solve();
return 0;
}
边栏推荐
- Wechat applet 8 cloud function
- Codeforces round 807 (Div. 2) e. mark and Professor Koro binary / segment tree
- 【微服务】 微服务学习笔记三:利用Feign替换RestTemplate完成远程调用
- Integrated video processing card based on Fudan micro FPGA + Huawei hisilic hi3531dv200 + Mji innovative MCU
- Chang'an chain learning research - storage analysis wal mechanism
- ORA-08103
- How to read and save point cloud data with numpy
- GYM103660L. Monster tower overall dichotomy
- Codeforces Round #807 (Div. 2) E. Mark and Professor Koro 二进制/线段树
- ZABBIX realizes the monitoring of redis
猜你喜欢

BigScience 开源 Bloom 的自然语言处理模型

session管理

session management

SBOM(Software Bill of Materials,软件物料清单)

国科大. 深度学习. 期末试题与简要思路分析

ICML2022 | 幾何多模態對比錶示學習

Single channel 6Gsps 12 bit AD acquisition and single channel 6Gsps 16 bit Da (ad9176) output sub card based on vita57.1 standard

Top domestic experts gathered in Guangzhou to discuss the safety application of health care data

ObjectARX -- implementation of custom circle

Google Earth engine - Classification and processing of UAV images
随机推荐
2. MySQL introduction
Leetcode 1296. 划分数组为连续数字的集合(提供一种思路)
2040: [蓝桥杯2022初赛] 砍竹子(优先队列)
马走斜日(回溯法)
Icml2022 | geometric multimodal comparative representation learning
vscod
人脸技术:不清楚人照片修复成高质量高清晰图像框架(附源代码下载)
SQL wrong questions set of Niuke brush questions
[code attached] how to realize handwritten digit recognition with hog+svm
UVA - 12096 The SetStack Computer
UCAS. Deep learning Final examination questions and brief thinking analysis
Mongodb partition cluster construction
一次函数 T1744 963字符写法
全排列(深度优先,排列树)
暑期第三周总结
【花雕动手做】有趣好玩的音乐可视化项目(11)---WS2812幻彩灯带
Tips for using setup
kube-proxy & Service & Endpoint
FMC sub card: 4-channel 12bit 3.2g, 2-channel 12bit, 6.4g AD acquisition / 5G acquisition card /6g acquisition card
Wechat applet 7 cloud storage