Codeforces Round #470 (rated, Div. 1, based on VK Cup 2018 Round 1) 923D 947D 948E D. Picking Strings
题:
OvO http://codeforces.com/contest/947/problem/D
923D 947D 948E
解:
记要改变的串为 P1 ,记目标串为 P2
由变化规则可得:
1. B -> AC -> AAB -> AAAC -> C ( 即 B -> C -> B )
2. AB -> AAC -> AAAB -> B (即 AB -> B )
3. B -> AC -> AB ( 即 B -> AB )
4. A -> BC -> BB ( 即 A -> BB )
(由规则1可得,B 与 C 等价,所以下文 C 全由 B代替)
那么对于一个串 S ,可以从这个串中提取 3 个特征,记这3个特征为 val0, val1, val2,
这3个特征的意义为:
对于串 S,把 S 串分成2个部分 S1, S2,其中 S2 是 S 的后缀,其内含字符全为 A,使 S2 长度尽可能长,记 S2 串长度为 val2 。S1 串为去掉 S2 后的部分,记 S1 串中非 A 字符的个数为 val1 。并且,如果 S1 串为空,记 val0=0,否则 val=1。
(接下去的思路就是转化 P1 中的 S1 与 S2,使其与 P2 中的 S1,S2 相同)
然后进行分类讨论,以下 5 种情况,P1 串无法转换为 P2 串(不具体证明)
1. (P1.val1 & 1) != (P2.val1 & 1)
2. P1.val1 > P2.val1
3. P1.val2 < P2.val2
4. P1.val1 == P2.val1 && (P1.val2 - P2.val2) % 3 != 0
5. P1.val0 == 0 && P1.val1 < P2.val1 && P1.val2 == P2.val2
其余情况,P1 均可以转化为 P2
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdio> using namespace std; struct Val
{
int val0,val1,val2;
} ; const int M=1e5+44; char s[2][M];
int len[2],p[2][M],a[2][M],lst[2][M],b[2][M];
int lans,ans[M]; void init(int id)
{
int tmp;
scanf("%s",s[id]+1);
len[id]=strlen(s[id]+1);
for(int i=1;i<=len[id];i++)
if(s[id][i]=='A') p[id][i]=0;
else p[id][i]=1;
a[id][0]=b[id][0];
for(int i=1;i<=len[id];i++)
a[id][i]=a[id][i-1]+(p[id][i]==0),b[id][i]=b[id][i-1]+(p[id][i]==1);
tmp=0;
for(int i=1;i<=len[id];i++)
if(p[id][i]==1)
tmp=i,lst[id][i]=tmp;
else lst[id][i]=tmp;
} Val solve(int id,int li,int ri)
{
int tag=lst[id][ri],part0,part1,part2;
if(tag<li) tag=li-1,part0=0;
else part0=1;
part2=(a[id][ri]-a[id][tag]);
part1=(b[id][ri]-b[id][li-1]);
return Val{part0,part1,part2};
} bool check(Val tp0,Val tp1)
{
if((tp0.val1&1)!=(tp1.val1&1)) return false;
if(tp0.val1>tp1.val1) return false;
if(tp0.val2<tp1.val2) return false;
if(tp0.val1==tp1.val1 && (tp0.val2-tp1.val2)%3!=0) return false;
if(tp0.val0==0 && tp0.val1<tp1.val1 && tp0.val2==tp1.val2) return false;
return true;
} int main()
{
init(0),init(1);
int cas,li0,ri0,li1,ri1;
Val tp[2];
lans=0;
scanf("%d",&cas);
while(cas--)
{
scanf("%d%d%d%d",&li0,&ri0,&li1,&ri1);
tp[0]=solve(0,li0,ri0),tp[1]=solve(1,li1,ri1);
// cout<<tp[0]<<" "<<tp[1]<<endl;
if(check(tp[0],tp[1])) ans[++lans]=1;
else ans[++lans]=0;
}
for(int i=1;i<=lans;i++)
printf("%d",ans[i]);
return 0;
}
Codeforces Round #470 (rated, Div. 1, based on VK Cup 2018 Round 1) 923D 947D 948E D. Picking Strings的更多相关文章
- Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)A. Protect Sheep
http://codeforces.com/contest/948/problem/A A. Protect Sheep Bob is a farmer. He has a large pastu ...
- Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1) C.Producing Snow
题目链接 题意 每天有体积为Vi的一堆雪,所有存在的雪每天都会融化Ti体积,求出每天具体融化的雪的体积数. 分析 对于第i天的雪堆,不妨假设其从一开始就存在,那么它的初始体积就为V[i]+T[1. ...
- Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)
A. Protect Sheep time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)B. Primal Sport
Alice and Bob begin their day with a quick game. They first choose a starting number X0 ≥ 3 and try ...
- Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) F 构造
http://codeforces.com/contest/967/problem/F 题目大意: 有n个点,n*(n-1)/2条边的无向图,其中有m条路目前开启(即能走),剩下的都是关闭状态 定义: ...
- Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) E 贪心
http://codeforces.com/contest/967/problem/E 题目大意: 给你一个数组a,a的长度为n 定义:b(i) = a(1)^a(2)^......^a(i), 问, ...
- Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) D 贪心
http://codeforces.com/contest/967/problem/D 题目大意: 有n个服务器,标号为1~n,每个服务器有C[i]个资源.现在,有两个任务需要同时进行,令他为x1,x ...
- 【枚举】【二分】Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) D. Resource Distribution
题意:有两个服务要求被满足,服务S1要求x1数量的资源,S2要求x2数量的资源.有n个服务器来提供资源,第i台能提供a[i]的资源.当你选择一定数量的服务器来为某个服务提供资源后,资源需求会等量地分担 ...
- 【推导】【贪心】Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2) D. Riverside Curio
题意:海平面每天高度会变化,一个人会在每天海平面的位置刻下一道痕迹(如果当前位置没有已经刻划过的痕迹),并且记录下当天比海平面高的痕迹有多少条,记为a[i].让你最小化每天比海平面低的痕迹条数之和. ...
随机推荐
- LeetCode 题目的 Python 实现(持续更新中)
Python-LeetCode 是一个使用 Python 语言解决 LeetCode 问题的代码库,库有以下几个方面需要注意: 所有题目都是 AC 的: 按照题目顺序,每 50 个放在一个目录下,方便 ...
- GB2312 字符集
<信息交换用汉字编码字符集>是由中国国家标准总局1980年发布,1981年5月1日开始实施的一套国家标准,标准号是GB 2312—1980. GB2312编码适用于汉字处理.汉字通信等系统 ...
- Ubuntu截图工具Flameshot
今天来介绍一款Ubuntu下的截图工具,名叫Flameshot. 安装 Flameshot的安装很简单. 命令行安装 sudo apt-get install flameshot 一条命令搞定! 软件 ...
- MongoDB journal 与 oplog,究竟谁先写入?--转载
MongoDB journal 与 oplog,谁先写入?最近经常被人问到,本文主要科普一下 MongoDB 里 oplog 以及 journal 这两个概念. journal journal 是 M ...
- SOLID Principles
Intention: more understandable, easier to maintain and easier to extend.(通过良好的设计使得代码easy and simple, ...
- map自定义键值类型
map自定义键值类型 改变Map的默认比较方式 https://www.cnblogs.com/zjfdlut/archive/2011/08/12/2135698.html 大家知道,STL中的ma ...
- luogu2657-Windy数题解--数位DP
题目链接 https://www.luogu.org/problemnew/show/P2657 分析 第一道数位DP题,发现有点意思 DP求\([L,R]\)区间内的XXX个数,很套路地想到前缀和, ...
- ASIHTTPRequest源码简单分析
1.前言 ASIHttprequest 是基于CFNetwork的,由于CFNetwork是比较底层的http库,功能比较少,因此,在ASIHttprequest中实现了http协议中比 ...
- c#开启线程池超出索引
这样写会超出索引,foreach好像不会超出,原因可能是开启线程池需要时间,成功开启之后,一次循环已经结束,这个没有验证. 以下这个做法是不对的,我也是看网上的贴这样写,结果是少执行了一个.推荐大家还 ...
- Redis 学习-Redis Sentinel
一.启动服务 1. 配置文件 sentinel.conf daemonize yes # 是否守护进程启动 pidfile "/var/run/redis-sentinel-26379.pi ...