洛谷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 ...
随机推荐
- grep精确匹配搜索某个单词的用法 (附: grep高效用法小结))
grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正 ...
- 分布式监控系统Zabbix--完整安装记录-批量添加主机和自动发现端口
一.Zabbix-3.0.3批量添加主机的配置如下: 0)被监控机上要安装zabbix_agent,并配置好zabbix_agentd.conf (如下172.29.8.50是zabbix_serve ...
- Git常用命令梳理
在日常的Git版本库管理工作中用到了很多操作命令,以下做一梳理: 查看分支列表,带有*的分支表示是当前所在分支 [root@115~~]#git branch 查看分支详细情况 (推荐这种方式) [r ...
- <构建之法>8,9,10章的读后感
第八章 这一章主要讲的是需求分析,主要介绍在客户需求五花八门的情况下,软件团队如何才能准确而全面地找到这些需求. 第九章 问题:我们现在怎样培养才能成为一名合格的PM呢? 第十章 问题:如果典型用户吴 ...
- JavaScript获取DOM节点
常用的方法有 document.getElementById("id"); document.getElementsByTagName('tagName'); document.g ...
- Consul vs. Zookeeper
https://www.consul.io/intro/vs/zookeeper.html 阿里不用zookeeper而是用consul,京东也在用consul.
- Java Heap Dump On OutOfMemoryError
-XX:+HeapDumpOnOutOfMemoryError Batch "C:\Program Files\Java\jdk1.8.0_162\bin\java.exe" -X ...
- What Is Apache Hadoop
What Is Apache Hadoop? The Apache™ Hadoop® project develops open-source software for reliable, scala ...
- CentOS7 卸载mariadb 安装mysql的过程:
1. 检查安装的mariadb rpm -qa |grep mariadb 得到已经安装的安装包 mariadb-libs-5.5.56-2.el7.x86_64mariadb-devel-5.5.5 ...
- spark中saveAsTextFile的错误
写了很简单的一段spark代码,将结果保存为windows本地文件,执行之后总是报错NullPointerException 查询之后 发现是本地缺少hadoop需要的一个文件所致 如果本地已经安装了 ...