[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 ...
随机推荐
- opencv-python保存视频
import cv2 class WVideoManager: def __init__(self, write_path: str, width: int, height: int, FPS: in ...
- 强化学习-linux安装gym、atari和box2d环境
安装gym和atari环境 pip3 install gym pip3 install gym[atari] pip3 install gym[accept-rom-license] 安装box2d环 ...
- nifi从入门到实战(保姆级教程)——环境篇
背景: 公司领导决定将各种基础数据的导入从代码中分离出来,用Apache Nifi替换.使开发者们更关注在业务上,而不用关心基础的由来. Apache Nifi对于整个团队都是一个全新的工具,之前大家 ...
- Spring Data JPA系列3:JPA项目中核心场景与进阶用法介绍
大家好,又见面了. 到这里呢,已经是本SpringData JPA系列文档的第三篇了,先来回顾下前面两篇: 在第1篇<Spring Data JPA系列1:JDBC.ORM.JPA.Spring ...
- 通过input的name属性取值
HTML中 Script中 根据选中的值,res也就拿到相应的值.
- Kafka ETL 之后,我们将如何定义新一代实时数据集成解决方案?
上一个十年,以 Hadoop 为代表的大数据技术发展如火如荼,各种数据平台.数据湖.数据中台等产品和解决方案层出不穷,这些方案最常用的场景包括统一汇聚企业数据,并对这些离线数据进行分析洞察,来达到辅助 ...
- Python 内置logging 使用详细讲
logging 的主要作用 提供日志记录的接口和众多处理模块,供用户存储各种格式的日志,帮助调试程序或者记录程序运行过程中的输出信息. logging 日志等级 logging 日志等级分为五个等级, ...
- html字符超长后自动隐藏
style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;"
- super详解(继承)
//在Java中,所有的类,都默认直接或者间接继承objec类// Person 人 :父类public class Person /*extends object*/ { public Person ...
- 5-16 Docker 容器 || Linux
Docker 简介 Linux简介 Linux和windows一样,都是操作系统 只不过windows更易用,适合家庭和非编程人员使用 如果用作服务器,尤其是java项目的服务器,Linux会更加合适 ...