[CF1538E] Funny Substrings (模拟)
题面
该场 Div. 3 最“难”的一道题:Funny Substrings
O
I
D
\tt OID
OID 队长喜欢玩字符串,因为
“
O
n
e
I
n
D
a
r
k
”
\tt “OneInDark”
“OneInDark” 是一个望而生畏的字符串。
O
I
D
\tt OID
OID 会进行
N
\tt N
N 次操作,每次操作形如以下两种之一:
x := S: x 是变量名称,S 是一个具体的字符串,:=符号两边都有空格。O
I
D
\tt OID
OID 把此时的 x 叫做
S
Y
SY
SY 变量。该操作意思是把 x 赋值为字符串 S。
x = a + b: x,a,b 是变量名称,=符号和+符号两边都有空格。O
I
D
\tt OID
OID 把此时的 x 也叫做
S
Y
SY
SY 变量。该操作意思是把 x 赋值为 a 和 b 首尾拼接起来的字符串。保证 a 和 b 在此操作之前作为
S
Y
SY
SY 变量出现过。
现在问你,在最后一次出现的
S
Y
SY
SY 变量储存的字符串中,字符串 haha 作为子串出现过多少次。比如:haha 在 hahahaha 中出现了 3 次。
一共
T
≤
1
0
3
\tt T\leq10^3
T≤103 组数据,每组数据中
1
≤
N
≤
50
\tt1\leq N\leq50
1≤N≤50,
1
≤
∣
S
∣
≤
5
\tt1\leq|S|\leq5
1≤∣S∣≤5 ,
1
≤
∣
变
量
名
∣
≤
5
\tt1\leq|变量名|\leq5
1≤∣变量名∣≤5。
题解
(我看这道题过的人最少,姑且认为它难吧)
不难发现,最终的字符串长度可能超过 long long 范围,因此,直接 string 模拟不是个好方法。
我们会发现,对于一个字符串,有用的信息可以整理成一个三元组
{
l
(
s
t
r
i
n
g
)
,
x
(
l
o
n
g
l
o
n
g
)
,
r
(
s
t
r
i
n
g
)
}
\tt\{l(string),x(long~long),r(string)\}
{l(string),x(long long),r(string)},分别表示:该字符串最左边 3 个(也许不足 3 个)字符,该字符串中 haha 的出现次数,该字符串最右边 3 个(也许不足 3 个)字符。
这个不难处理。关键是这样的三元组可以方便地定义 a + b 操作,返回另一个三元组:
- 把
x
a
\tt x_a
xa 和
x
b
\tt x_b
xb 加起作为
x
\tt x
x ,再把
r
a
\tt r_a
ra 和
l
b
\tt l_b
lb 拼起来(长度不超过 6),统计里面的
haha数量,也加到x
\tt x
x 里面去。
- 把
l
\tt l
l 赋为
l
a
\tt l_a
la ,如果长度不足 3,再把
l
b
\tt l_b
lb 拼在
l
\tt l
l 后面,把长度超过 3 的砍掉。
- 把
r
\tt r
r 赋为
r
b
\tt r_b
rb ,如果长度不足 3,再把
r
a
\tt r_a
ra 拼在
r
\tt r
r 前面,把长度超过 3 的砍掉。
这样就做出这道模拟题了,中间的字符串操作,可以暴力用 string 或者 char*+sprintf,时间足够。对于每个变量怎么存三元组,你可以用哈希也可以用 map。
CODE
#include<set>
#include<map>
#include<queue>
#include<ctime>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXN 105
#define ENDL putchar('\n')
#define LL long long
#define DB double
#define lowbit(x) ((-x) & (x))
#define eps 1e-9
LL read() {
LL f = 1,x = 0;char s = getchar();
while(s < '0' || s > '9') {if(s=='-')f = -f;s = getchar();}
while(s >= '0' && s <= '9') {x=x*10+(s-'0');s = getchar();}
return f * x;
}
int n,m,i,j,s,o,k;
struct it{
char l[5],r[5];
LL nm;it(){memset(l,0,sizeof(l));memset(r,0,sizeof(r));nm=0;}
it(char *L,char *R,LL N){
strncpy(l,L,3);nm=N;memset(r,0,sizeof(r));
int le = strlen(R);
for(int i = max(le-3,0),j=0;i < le;i ++,j ++) r[j]=R[i];
}
};
it operator + (it a,it b) {
char ss[10]; int ls; LL as = a.nm+b.nm;
sprintf(ss,"%s%s",a.r,b.l);
ls = strlen(ss);
for(int i = 0;i <= ls-4;i ++) if(ss[i]=='h'&&ss[i+1]=='a'&&ss[i+2]=='h'&&ss[i+3]=='a') as ++;
char rel[10],rer[10];
if(strlen(a.l) < 3) sprintf(rel,"%s%s",a.l,b.l);
else strcpy(rel,a.l);
if(strlen(b.r) < 3) sprintf(rer,"%s%s",a.r,b.r);
else strcpy(rer,b.r);
return it(rel,rer,as);
}
map<string,it> mp;
int main() {
int T = read();
while(T --) {
n = read();
mp.clear();
string las;
for(int i = 1;i <= n;i ++) {
string A,md,B,C;
cin>>A;
las = A;
cin>>md;
if(md[0]==':') {
char ss[15]; int le,as = 0;
scanf("%s",ss); le = strlen(ss);
for(int i = 0;i <= le-4;i ++) {
if(ss[i]=='h'&&ss[i+1]=='a'&&ss[i+2]=='h'&&ss[i+3]=='a') as ++;
}
mp[A] = it(ss,ss,(LL)as);
}
else {
cin>>B;cin>>md;cin>>C;
mp[A] = mp[B] + mp[C];
}
}
printf("%lld\n",mp[las].nm);
}
return 0;
}
[CF1538E] Funny Substrings (模拟)的更多相关文章
- CodeForces 550A Two Substrings(模拟)
[题目链接]click here~~ [题目大意]: You are given string s. Your task is to determine if the given string s ...
- 【CF1029A】Many Equal Substrings(模拟)
题意:给定一个长度为n的串s,要求构造一个长度最小的使s出现了k次的串,可以重叠 n<=50,k<=50 思路:计算一下前后缀相同长度 #include<cstdio> #in ...
- poj 3415 Common Substrings(后缀数组+单调栈)
http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K Total Sub ...
- Advanced Fruits(好题,LCS的模拟)
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- CF 319D(Have You Ever Heard About the Word?-模拟)
D. Have You Ever Heard About the Word? time limit per test 6 seconds memory limit per test 256 megab ...
- HDU 5908 Abelian Period (BestCoder Round #88 模拟+暴力)
HDU 5908 Abelian Period (BestCoder Round #88 模拟+暴力) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=59 ...
- Codeforces 626A Robot Sequence(模拟)
A. Robot Sequence time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- Codeforces Round #533 (Div. 2) B. Zuhair and Strings 【模拟】
传送门:http://codeforces.com/contest/1105/problem/B B. Zuhair and Strings time limit per test 1 second ...
- hdu4886 TIANKENG’s restaurant(Ⅱ) (trie树或者模拟进制)
TIANKENG’s restaurant(Ⅱ) Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 130107/65536 K (Ja ...
随机推荐
- CSS 技术
浏览本篇文章前可以先看之前的前端网页介绍和html常用标签以便更容易理解 本文目录: 目录 CSS 技术介绍 CSS 语法规则 CSS 和 HTML 的结合方式 第一种: 第二种 第三种 CSS 选择 ...
- 批处理(bat、cmd)命令总结
2021-07-21 初稿 注释与回显 rem 回显 @取消单行回显 rem 注释有三种方式 :: %content% rem rem @取消单行回显,echo off取消后面的回显 @echo of ...
- 利用XtraDiagram.DiagramControl进行流程图形的绘制和控制
DevExpress提供了一个比较强大的图形绘制工具,可以用于绘制各种图形,如流程图.组织机构图等等,本篇随笔介绍XtraDiagram.DiagramControl的使用,以及利用代码对其属性进行控 ...
- DirectX11 With Windows SDK--06 使用ImGui
前言 Dear ImGui是一个开源GUI框架.除了UI部分外,本身还支持简单的键鼠交互.目前项目内置的是V1.87版本,大概半年时间会更新一次版本,并且对源码有小幅度调整. 注意:直接下载源码使用会 ...
- C#中的 Attribute 与 Python/TypeScript 中的装饰器是同个东西吗
前言 最近成功把「前端带师」带入C#的坑(实际是前端带师开始从cocos转unity游戏开发了) 某天,「前端带师」看到这段代码后问了个问题:[这个是装饰器]? [HttpGet] public Re ...
- RPA-UiPath视频教程1
UiPath下载.安装.激活.第一个案例Helloworld!.参数类型.变量的介绍和使用 https://www.bilibili.com/video/av92816532 RPA直播公开课2020 ...
- 论文阅读 GloDyNE Global Topology Preserving Dynamic Network Embedding
11 GloDyNE Global Topology Preserving Dynamic Network Embedding link:http://arxiv.org/abs/2008.01935 ...
- 基于UniApp社区论坛多端开发实战
什么是移动端WebApp 移动端WebApp: 泛指手持设备移动端的web 特点: - 类App 应用,运行环境是浏览器 - 可以包一层壳,成为App - 常见的混合应用: ionic, Cordov ...
- Graphics2D类
Graphics2D类 Java语言在Graphics类提供绘制各种基本的几何图形的基础上,扩展Graphics类提供一个Graphics2D类,它拥用更强大的二维图形处理能力,提供.坐标转换.颜色管 ...
- ESP分区重建,解决各种引导问题
电脑装了双系统,win7和win10,每次重启都进入不同系统,郁闷至极,索性把不常用的Win7盘格式化,但依旧解决不了问题.所以有了以下方法. 1.进PE删除ESP分区(先备份). 2.新建ESP分区 ...