洛谷P3952 时间复杂度
大毒瘤......
时隔快半年我终于花了两个小时堪堪A掉这一题...果然我还没有准备好。
想法:用DFS模拟递归。
时间复杂度的处理:每层循环取max,然后相加。
最大难点:各种繁杂而令人发指的特判。
为了简明代码,我写了6个辅助函数。
然后就A了...
// shi jian fu za du
#include <cstdio>
#include <algorithm>
#include <string>
#include <iostream> const int ERR = -; int L, vis[]; inline int getOP() { /// F 0 E 1
L--;
char c = getchar();
while(c != 'E' && c != 'F') {
c = getchar();
}
return c == 'E';
}
inline int getName() {
char c = getchar();
while(c < 'a' || c > 'z') {
c = getchar();
}
return c - 'a';
}
inline int getSum() {
int Sum = ;
char c = getchar();
while(c != 'n' && (c < '' || c > '')) {
c = getchar();
}
if(c == 'n') {
return -;
}
while(c >= '' && c <= '') {
Sum = (Sum << ) + (Sum << ) + c - ;
c = getchar();
}
return Sum;
}
inline void getrest() {
while(L) {
int t = getOP();
if(!t) {
t = getName();
t = getSum();
t = getSum();
}
}
return;
}
inline int get(std::string s) {
if(s[] == '') {
return ;
}
int ans = ;
for(int i = ; i < s.size() - ; i++) {
ans = (ans << ) + (ans << ) + s[i] - ;
}
return ans;
}
inline int gettime(int a, int b) {
if(a == - && b == -) {
return ;
}
else if(a == -) {
return -;
}
else if(b == -) {
return ;
}
else if(a <= b) {
return ;
}
return -;
}
//
inline int DFS(int k, int time) {
int ans = , nex_time;
while(L) {
int t = getOP();
if(t == ) {
if(!k) {
getrest();
return ERR;
}
break;
}
/// F
int name = getName();
int a = getSum();
int b = getSum();
if(vis[name]) {
getrest();
return ERR;
}
if(!L) {
return ERR;
}
vis[name] = ;
nex_time = gettime(a, b); t = DFS(k + , nex_time);
vis[name] = ;
if(t == ERR) {
return ERR;
}
if(!L && k) {
return ERR;
}
ans = std::max(ans, t);
}
if(time == -) {
return ;
}
return time + ans;
} int main() {
int T;
scanf("%d", &T);
std::string s;
while(T--) {
scanf("%d", &L);
std::cin >> s;
int t = get(s);
int g = DFS(, );
if(g == ERR) {
printf("ERR\n");
}
else if(g == t) {
printf("Yes\n");
}
else {
printf("No\n");
}
}
return ;
}
AC代码
[update 2018.10.11]
额,最近练模拟,想起来这道题。因为以前做过一次所以很轻易就A了。
大概十几分钟打完(记得之前是怎么处理的),第一次交80分,还是个老问题:没有把之前剩余的语句读完。
还需要注意对第一次操作的处理:结束时你没有一个多余的E让你出来,你要自己特判L = 0,然后如果不在最外层的话就是error。
#include <cstdio>
#include <map>
#include <cstring> std::map<char, bool> mp;
char s[];
int L, err; inline int read_time() {
scanf("%s", s);
if(s[] == '') {
return ;
}
int x = ;
for(int i = ; i < strlen(s) - ; i++) {
x = (x << ) + (x << ) + s[i] - ;
}
return x;
} inline int read_op() {
L--;
scanf("%s", s);
return s[] == 'E';
} inline int read(char &c) {
scanf("%s", s);
c = s[];
scanf("%s", s);
int a = , b = ;
if(s[] == 'n') {
a = ;
}
else {
for(int i = ; i < strlen(s); i++) {
a = (a << ) + (a << ) + s[i] - ;
}
}
scanf("%s", s);
if(s[] == 'n') {
b = ;
}
else {
for(int i = ; i < strlen(s); i++) {
b = (b << ) + (b << ) + s[i] - ;
}
}
if(a == ) {
if(b == ) {
return ;
}
else {
return -;
}
}
if(b == ) {
return ;
}
if(a > b) {
return -;
}
return ;
} int solve(int k, char c) {
int large = ;
char j;
while() {
if(!L) {
if(c != '') {
err = ;
}
break;
}
int f = read_op();
if(f) {
break;
}
int temp = read(j);
if(mp[j]) {
err = ;
}
mp[j] = ;
large = std::max(large, solve(temp, j));
}
mp[c] = ;
if(k == -) {
return ;
}
return large + k;
} inline void work() {
scanf("%d", &L);
err = ;
mp.clear();
int time = read_time();
int ans = solve(, '');
if(L) {
err = ;
while(L) {
if(!read_op()) {
read(s[]);
}
}
}
if(err) {
printf("ERR\n");
return;
}
if(ans == time) {
printf("Yes\n");
}
else {
printf("No\n");
}
return;
} int main() {
int T;
scanf("%d", &T);
while(T--) {
work();
}
return ;
}
AC代码
洛谷P3952 时间复杂度的更多相关文章
- 洛谷 P3952 时间复杂度 解题报告
P3952 时间复杂度 题目描述 小明正在学习一种新的编程语言A++,刚学会循环语句的他激动地写了好多程序并 给出了他自己算出的时间复杂度,可他的编程老师实在不想一个一个检查小明的程序, 于是你的机会 ...
- 洛谷P3952 时间复杂度【字符串】【模拟】
题目描述 小明正在学习一种新的编程语言 A++,刚学会循环语句的他激动地写了好多程序并 给出了他自己算出的时间复杂度,可他的编程老师实在不想一个一个检查小明的程序, 于是你的机会来啦!下面请你编写程序 ...
- 洛谷 - P3952 - 时间复杂度 - 模拟
https://www.luogu.org/problemnew/show/P3952 这个模拟,注意每次进入循环的时候把新状态全部入栈,退出循环的时候就退栈. 第一次就错在发现ERR退出太及时,把剩 ...
- 计蒜客 时间复杂度 (模拟) & 洛谷 P3952 时间复杂度
链接 : Here! 思路 : 这是一道大模拟, 区分好情况就没问题了 循环构成部分 : $F , x , i , j$ 和 $E$ , 需要注意的是 $i , j$, - 分析 $i, j$ 的情况 ...
- 洛谷 P3952时间复杂度 (本地AC测评RE的伪题解)
[题目描述] 小明正在学习一种新的编程语言 A++,刚学会循环语句的他激动地写了好多程序并 给出了他自己算出的时间复杂度,可他的编程老师实在不想一个一个检查小明的程序, 于是你的机会来啦!下面请你编写 ...
- 2018.11.02 洛谷P3952 时间复杂度(模拟)
传送门 惊叹考场dubuffdubuffdubuff. 这题还没有梭哈难啊233. 直接按照题意模拟就行了. 代码: #include<bits/stdc++.h> using names ...
- 洛谷P3952 时间复杂度(模拟)
题意 题目链接 Sol 咕了一年的题解..就是个模拟吧 考场上写的递归也是醉了... 感觉一年自己进步了不少啊..面向数据编程的能力提高了不少 #include<bits/stdc++.h> ...
- 洛谷 P3952 时间复杂度【模拟】
把No写成NO,WA了一发-- 现在看这题也不难-- 用一个栈,记一下前面F的字母,是否合法,合法的有多长,每次入栈弹栈即可 #include<iostream> #include< ...
- 【题解】洛谷P3952 [NOIP2017TG] 时间复杂度(模拟)
题目来源:洛谷P3952 思路 纯模拟没啥可说的了 果然好复杂 参考了你谷一个40行代码 代码 #include<iostream> #include<cstdio> #inc ...
随机推荐
- 利用 John the Ripper 破解用户登录密码
一.什么是 John the Ripper ? 看到这个标题,想必大家都很好奇,John the Ripper 是个什么东西呢?如果直译其名字的话就是: John 的撕裂者(工具). 相比大家都会觉得 ...
- [朴智妍][Lullaby]
歌词来源:http://music.163.com/#/song?id=484056971 作曲 : Bum/Sophiya/김용신 [作曲 : Bum/Sophiya/k/gi-myong-xin] ...
- Visual Studio平台安装及测试
一.VS安装 图1.1 图1.2 二.单元测试练习 题目:课本22~25页单元测试练习 1.创建一个c#类(具体如下:打开VS2010,然后点击VS界面上左上角的文件按钮,然后点击文件—新建—项目,就 ...
- WIFI探针技术
1.WIFI 探针定义 WIFI 探针是一种能够主动识别 Android 和 IOS 设备,感知用户行为轨迹的精准数据收集前端,基于 WIFI探测技术.移动互联网和云计算等先进技术自动识别探针附近的智 ...
- SPRINT四则运算(第二天)
1.小组成员: 李豌湄:master 江丹仪:产品负责人 2.现状: a.已经下载APP分析他们的界面.优缺点和闪光点 b.已改进代码添加功能 3.任务认领: 完成任务的第一个模块: a.下载五个类 ...
- weex 开发踩坑日记--环境配置、安卓运行、adb、开发
环境配置方面 1.需要安装java和android环境,java的话一定要下载jdk而不是jre. 在"系统变量"新建一个变量名为JAVA_HOME的变量,变量值为你本地java的 ...
- RequestHolder工具类
package com.inspire.ssm.common; import com.inspire.ssm.model.SysUser; import javax.servlet.http.Http ...
- java mail session使用Properties的clone方法
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreem ...
- docker:Dockerfile构建LNMP平台
docker:Dockerfile构建LNMP平台 1.dockerfile介绍 Dockerfile是Docker用来构建镜像的文本文件,包含自定义的指令和格式.可以通过docker buil ...
- [转帖] select、poll、epoll之间的区别总结[整理] + 知乎大神解答 https://blog.csdn.net/qq546770908/article/details/53082870 不过图都裂了.
select.poll.epoll之间的区别总结[整理] + 知乎大神解答 2016年11月08日 15:37:15 阅读数:2569 http://www.cnblogs.com/Anker/p/3 ...