当前位置:网站首页>【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;
}
边栏推荐
- Maximum heap and heap sort and priority queue
- session管理
- 07_服务注册与发现总结
- 论文阅读 TEMPORAL GRAPH NETWORKS FOR DEEP LEARNING ON DYNAMIC GRAPHS
- 微信小程序9-发布代码
- Unveil the mystery of service grid istio service mesh
- C - matrix chain multiplexing (Application of stack)
- 长安链学习研究-存储分析wal机制
- ICML2022 | 幾何多模態對比錶示學習
- UVA340 Master-Mind Hints
猜你喜欢

GYM103660H. Distance

通过授权微信,达到软件登录账号的效果~~未完

08_服务熔断Hystrix

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

Integrated video processing card based on Fudan micro FPGA + Huawei hisilic hi3531dv200 + Mji innovative MCU

1. Basic concepts of DBMS

1、DBMS基本概念

ORA-00054

csrf防护机制

Chang'an chain learning research - storage analysis wal mechanism
随机推荐
UVA340 Master-Mind Hints
Wechat applet 8 cloud function
UVA - 12096 The SetStack Computer
Codeforces round 807 (Div. 2) e. mark and Professor Koro binary / segment tree
分布式事务总结
Leetcode 1296. Divide the array into a set of consecutive numbers (solved)
kibana 使用json文档数据
P1004 [noip2000 improvement group] grid access
Mongodb partition cluster construction
06_服务调用Feign
CSRF protection mechanism
Module 1 job
原始套接字
2034: [蓝桥杯2022初赛] 修剪灌木
[code attached] how to realize handwritten digit recognition with hog+svm
微信小程序8-云函数
A - Play on Words
FPGA (VGA Protocol Implementation)
Scheduled tasks, VIM directly creates and modifies users
session management