【luogu P3952 时间复杂度】 题解
对于2017 D1 T2 这道题
实实在在是个码力题,非常考验耐心。
其实大体的思路并不是非常难想出来,但是要注意的小细节比较多。
题目链接:https://www.luogu.org/problemnew/show/P3952
思路
对于每一个程序,先读入L和O(),并将其中的时间复杂度抠出来。
其次整行读入字符串,即所给定的程序。
判断第一个字符是F or E
F i x y 需要把x y拿出来,把i压进栈
E 退栈 压进i后为了方便退栈及退栈时判断,用一个flag标记
每做完一个程序,与前面抠出来的时间复杂度对比判断yesnoerr即可。
注意
1.我的readx和ready函数比较暴力,直接截取常数和n可能存在的位置并保存。所以在考虑情况时,常数与n的位置不能搞错,也不能少考虑。
2.在每搞完一个程序时,要把初始值和标记都初始化一遍。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
int L,t,anso,codeo;//anso=0 O(1) anso=x O(n^x) 输入给的时间复杂度 codeo 自己算的时间复杂度 if anso==codeo yes else no if codeo==-1 err
string code[];//按行输入所给代码(F,E)
string ans;//读入给定的复杂度 O()
int readx(string x)
{
int num;
if(x[] >= '' && x[] <= '') num = x[]-'';
if(x[] >= '' && x[] <= '') num = (x[]-'')*+x[]-'';
if(x[] == 'n') num = ;
return num;
} //抠出给定的复杂度 x
int ready(string x)
{
int num;
if(x[] >= '' && x[] <= '' && x[] <= '' || x[] >= '') num = x[]-'';
if(x[] >= '' && x[] <= '' && x[] <= '' || x[] >= '') num = x[]-'';
if(x[] >= '' && x[] <= '' && x[] >= '' && x[] <= '') num = (x[]-'')*+x[]-'';
if(x[] >= '' && x[] <= '' && x[] >= '' && x[] <= '') num = (x[]-'')*+x[]-'';
if(x[] == 'n') num = ;
if(x[] == 'n') num = ;
return num;
}//抠出给定的复杂度 y
int check1()
{
int res;
if(ans[] == '') res = ;
if(ans[] == 'n')
{
if(ans[]<='' && ans[]>='')
res = ans[]-'';
if(ans[]<='' && ans[]>='')
res = (ans[]-'')* + ans[]-'';
}//记录输入给的复杂度
return res;
}
int check2()
{
stack<int> s;
int flag=-;//标记
bool fe[]={};//上面都是便于栈操作,fe来记录变量名
int res=,now=;//now来记录当前循环中的时间复杂度,res是整个程序的时间复杂度
bool cflag[]={};//记录变量是否重复 0 则没用过 1 用过 changeflag
int xnum,ynum; for(int i=;i<=L;i++)
{ if(code[i][]=='F')
{
int k=code[i][]-'a';
if(cflag[k]) return -;
s.push(k);
cflag[k] = ; xnum=readx(code[i]); ynum=ready(code[i]);
if(ynum-xnum>)
{
if(flag==-)
{
now++;
res=max(res,now);
fe[k]=;
}
}
if(xnum>ynum)
{
if(flag==-) flag=k;
}
} if(code[i][]=='E')
{
if(s.empty()) return -;
int k=s.top();
s.pop();cflag[k]=false;
if(flag==k) flag=-;
if(fe[k])
{
fe[k]=false;
now--;
}
}
}
if(s.size()) return -;
return res;
}
int main()
{
scanf("%d",&t);
while(t--)
{ scanf("%d ",&L);getline(cin,ans);
anso=check1(); for(int i=;i<=L;i++)getline(cin,code[i]);
codeo=check2(); if(codeo==-) cout<<"ERR"<<endl;
else
{
if(anso!=codeo) cout<<"No"<<endl;
if(anso==codeo) cout<<"Yes"<<endl;
}
}
return ;
}
【luogu P3952 时间复杂度】 题解的更多相关文章
- luogu P3952 时间复杂度 模拟
题目链接 luogu P3952 时间复杂度 题解 直接模拟即可 注意不要直接return 我真是naive ...... 代码 #include<map> #include<sta ...
- [LUOGU] P3952 时间复杂度
其实,也没那么难写 这种模拟题,仔细分析一下输入格式,分析可能的情况,把思路写在纸上,逐步求精,注意代码实现 主要思路就是算一个时间复杂度,和给出的复杂度比较,这就先设计一个函数把给出的复杂度由字符串 ...
- [NOIp2017] luogu P3952 时间复杂度
跪着看评测很优秀. 题目描述 给你若干个程序,这些程序只有 For 循环,求这些程序的时间复杂度. Solution 大模拟.讲下细节. flag[i]flag[i]flag[i] 表示第 iii 位 ...
- P3952 时间复杂度
P3952 时间复杂度 题目描述 小明正在学习一种新的编程语言 A++,刚学会循环语句的他激动地写了好多程序并 给出了他自己算出的时间复杂度,可他的编程老师实在不想一个一个检查小明的程序, 于是你的机 ...
- 洛谷 P3952 时间复杂度 解题报告
P3952 时间复杂度 题目描述 小明正在学习一种新的编程语言A++,刚学会循环语句的他激动地写了好多程序并 给出了他自己算出的时间复杂度,可他的编程老师实在不想一个一个检查小明的程序, 于是你的机会 ...
- 洛谷 P3952时间复杂度 (本地AC测评RE的伪题解)
[题目描述] 小明正在学习一种新的编程语言 A++,刚学会循环语句的他激动地写了好多程序并 给出了他自己算出的时间复杂度,可他的编程老师实在不想一个一个检查小明的程序, 于是你的机会来啦!下面请你编写 ...
- 洛谷 - P3952 - 时间复杂度 - 模拟
https://www.luogu.org/problemnew/show/P3952 这个模拟,注意每次进入循环的时候把新状态全部入栈,退出循环的时候就退栈. 第一次就错在发现ERR退出太及时,把剩 ...
- 洛谷P3952 时间复杂度【字符串】【模拟】
题目描述 小明正在学习一种新的编程语言 A++,刚学会循环语句的他激动地写了好多程序并 给出了他自己算出的时间复杂度,可他的编程老师实在不想一个一个检查小明的程序, 于是你的机会来啦!下面请你编写程序 ...
- LOJ P3952 时间复杂度 noip 暴力 模拟
https://www.luogu.org/problemnew/show/P3952 模拟,日常认识到自己zz. #include<iostream> #include<cstdi ...
随机推荐
- Task的一些用法总结
一.Task和多线程以及异常的捕获示例代码: static void Main(string[] args) { // 产生CancellationToken的类,该类允许使用Cancel方法终止线程 ...
- 数组和json的相互转换
json_encode() <?php /*****一维数组*********/ //有键 $arr = array( 'a'=>1, 'b'=>2, 'c'=>3, ); $ ...
- pat00-自测2. 素数对猜想 (20)
00-自测2. 素数对猜想 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 让我们定义 dn 为:dn ...
- HRBUST 1909——理工门外的树——————【离线处理,差分前缀和】
理工门外的树 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %lld , %llu Java class n ...
- nyoj 1192——Salvation——————【搜索】
Salvation 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 神秘瀑布镇是一个神秘的地方,那里有吸血鬼,狼人,巫师,二重身. Klaus(吸血鬼祖先) 为了利用 ...
- nyoj 546——Divideing Jewels——————【dp、多重背包板子题】
Divideing Jewels 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Mary and Rose own a collection of jewells. ...
- HDU 1698——Just a Hook——————【线段树区间替换、区间求和】
Just a Hook Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- java去掉String里面的空格、换行符等
package com.ynet.utils; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Create ...
- Array、ArrayList和List三者的区别
数组 class TestArraysClass { static void Main() { // Declare a single-dimensional array ]; // Declare ...
- 远程SQL Server连接不上
运行 cmd -> 输入 netsh winsock reset重启后 应该可以连接sql了