洛谷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 ...
随机推荐
- 开启mac上印象笔记的代码块
Mac 印象笔记左上角菜单栏:偏好设置-->软件更新-->开启代码块 (Preferences -> Software Update -> Enable code block) ...
- Session之Config配置
<sessionState mode="Off|InProc|StateServer|SQLServer" cookieless="true|false" ...
- Gitlab环境快速部署(RPM包方式安装)
之前梳理了一篇Gitlab的安装CI持续集成系统环境---部署Gitlab环境完整记录,但是这是bitnami一键安装的,版本比较老.下面介绍使用rpm包安装Gitlab,下载地址:https://m ...
- Mongodb主从复制/ 副本集/分片集群介绍
前面的文章介绍了Mongodb的安装使用,在 MongoDB 中,有两种数据冗余方式,一种 是 Master-Slave 模式(主从复制),一种是 Replica Sets 模式(副本集). Mong ...
- Buy the Ticket HDU 1133
传送门 [http://acm.hdu.edu.cn/showproblem.php?pid=1133] 题目描述和分析 代码 #include<iostream> #include< ...
- Scrum Meeting NO.7
Scrum Meeting No.7 1.会议内容 经过老师提醒,我们认识到,应尽快把主要功能实现,其他的细枝末节应在这之后慢慢添加.当今最重要的任务是和online组和数据处理组实现数据共享. 此外 ...
- 常见IP端口
21端口:21端口主要用于FTP(File Transfer Protocol,文件传输协议)服务. 23端口:23端口主要用于Telnet(远程登录)服务,是Internet上普遍采用的登录和仿真程 ...
- Leetcode 546. Remove Boxes
题目链接: https://leetcode.com/problems/remove-boxes/description/ 问题描述 若干个有序排列的box和它们的颜色,每次可以移除若干个连续的颜色相 ...
- 伪静态与重定向--RewriteRule
环境:windows 10,phpstudy,sublime text.服务器使用Apache,网站根目录为E:\phpstudy\www\,所以.htaccess放在www目录下. RewriteR ...
- 变更RHEL(Red Hat Enterprise Linux 5.8)更新源使之自动更新
HP 4411s Install Red Hat Enterprise Linux 5.8) pick up from http://blog.chinaunix.net/uid-423637-id- ...