NOIP2017 D1T2 时间复杂度

14 篇文章 0 订阅
1 篇文章 0 订阅

时间复杂度

题目背景:

NOIP2017 D1T2

分析:模拟 + 

 

讲真因为T1有点影响心情,考场上打这个题的时候有点混乱,不过好歹最后A了,虽然代码各种不能看,其实就相当于一个人工栈,一个F表示加入,E表示弹出,这样最后可以比较方便的判定非法,然后中途就是判定有多少个n,和多少个常数复杂度。然后判定一下n的个数对不对就好了,因为自己人比较zz,所以实现非常繁琐······

Source

/*
	created by scarlyw
*/
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cctype>
#include <queue>
#include <vector>

template<class T>
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = getchar(), iosig = false; !isdigit(c); c = getchar()) {
		if (c == -1) return ;
		if (c == '-') iosig = true;
	}
	for (x = 0; isdigit(c); c = getchar())
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}

char c[10], temp;
bool flag;
bool vis[30];
int x, y, cnt, l, t;

inline int dfs(int cnt, int num, int cc) {
	int max = 0;
	char temp = 0;
	while (true) {
		temp = 0;
		while (temp != 'F' && temp != 'E') temp = getchar();
		::cnt++;
		if (temp == 'E') {
			vis[cc] = false;
			return max * num + cnt;
		}
		char x = 0;
		while (x > 'z' || x < 'a') x = getchar();
		if (vis[x - 'a'] == true) flag = false;
		else vis[x - 'a'] = true;
		char a[10], b[10];
		scanf("%s%s", a, b);
		int d = -1;
		if (a[0] == 'n' && b[0] == 'n') d = 1;
		else if (a[0] == 'n' && b[0] != 'n') d = -1;
		else if (a[0] != 'n' && b[0] == 'n') d = 2;
		else if (a[0] != 'n' && b[0] != 'n') {
			int s = 0, t = 0, len;
			len = strlen(a);
			for (int j = 0; j < len; ++j) s = (s * 10) + (a[j] ^ '0');
			len = strlen(b);
			for (int j = 0; j < len; ++j) t = (t * 10) + (b[j] ^ '0');
			if (s <= t) d = 1;
			else d = -1;
		}
		if (::cnt == l) {
			flag = false;
			return 0;
		}
		if (d == -1) max = std::max(max, dfs(0, 0, x - 'a'));
		else if (d == 1) max = std::max(max, dfs(0, 1, x - 'a'));
		else if (d == 2) max = std::max(max, dfs(1, 1, x - 'a'));
		if (::cnt == l) {
			flag = false;
			return 0;
		}
	}
}

inline void solve() {
	R(l), flag = true, cnt = 0, temp = 0;
	memset(vis, 0, sizeof(vis));
	while (temp != '1' && temp != 'n') temp = getchar();
	if (temp == '1') y = 0;
	else R(y);
	int ans = 0;
	for (cnt = 1; cnt <= l; cnt++) {
		temp = 0;
		while (temp != 'F' && temp != 'E') temp = getchar();
		if (temp == 'E') {
			flag = false;
			continue ;
		}
		char x = 0;
		while (x > 'z' || x < 'a') x = getchar();
		if (vis[x - 'a'] == true) flag = false;
		else vis[x - 'a'] = true;
		char a[10], b[10];
		scanf("%s%s", a, b);
		int d = -1;
		if (a[0] == 'n' && b[0] == 'n') d = 1;
		else if (a[0] == 'n' && b[0] != 'n') d = -1;
		else if (a[0] != 'n' && b[0] == 'n') d = 2;
		else if (a[0] != 'n' && b[0] != 'n') {
			int s = 0, t = 0, len;
			len = strlen(a);
			for (int j = 0; j < len; ++j) s = (s * 10) + (a[j] ^ '0');
			len = strlen(b);
			for (int j = 0; j < len; ++j) t = (t * 10) + (b[j] ^ '0');
			if (s <= t) d = 1;
			else d = -1;
		}
		if (cnt == l) {
			flag = false;
			break ;
		}
		if (d == -1) ans = std::max(ans, dfs(0, 0, x - 'a'));
		else if (d == 1) ans = std::max(ans, dfs(0, 1, x - 'a'));
		else ans = std::max(ans, dfs(1, 1, x - 'a'));
		if (cnt == l) break ;
	}
	if (flag == false) std::cout << "ERR\n";
	else if (ans == y) std::cout << "Yes\n";
	else std::cout << "No\n";
}

int main() {
	freopen("complexity.in", "r", stdin);
	freopen("complexity.out", "w", stdout);
	R(t);
	while (t--) solve();
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值