CCF刷题-0818

201709-3 json查询
感觉第三题越来越难了,好麻烦,好复杂
不会写,懒得思考
看的题解,为大佬点赞

#include <cstdio>
#include <iostream>
#include <map>
#include <string>
using namespace std;

map<string, string> json;

// 从下标i开始,寻找键名或者键值,并去掉首尾的引号、中间的转义符号'\'
string get(int &i, string str) {
	string key;
	i++;
	while (i < str.size() && str[i] != '"') {
		// 若遇到转义符号,则跳过一位
		if (str[i] == '\\') {
			key += str[++i];
		} else {
			key += str[i];
		}
		i++;
	}
	return key;
}

// 解析json字符串,parent表示当前处理的json串是哪个键的值,
// str为尚未解析的字符串,返回值为本次解析的json串的长度
int parseJSON(string parent, string str) {

	string key;
	for (int i = 0; i < str.size(); i++) {

		// 遇到右花括号},表示当前需要处理的json串已经结束
		if (str[i] == '}') {
			return i;
		}

		if (str[i] == ' ' || str[i] == ',') {
			continue;
		}
		// 获取键名
		if (str[i] == '"') {
			key = get(i, str);
			continue;
		}
		// 获取键值
		if (str[i] == ':') {
			// 去掉可能存在的空格
			while (str[++i] == ' ') {}
			// 若值为字符串
			if (str[i] == '"') {
				string value = get(i, str);
				if (!parent.empty()) {
					json[parent + '.' + key] = value;
				} else {
					json[key] = value;
				}
				continue;
			}
			// 若值为对象
			// 去掉左花括号{后面可能存在的空格
			while (str[++i] == ' ') {}
			string newParent;
			if (!parent.empty()) {
				newParent = parent + '.' + key;
			} else {
				newParent = key;
			}
			// 用{}标记newParent的值为对象
			json[newParent] = "{}";
			// 解析子串
			i += parseJSON(newParent, str.substr(i));
		}
	}
	return str.size();
}

int main() {
	int n, m;
	cin >> n >> m;
	getchar();

	string str = "";
	string line;
	while (n--) {
		getline(cin, line);
		str += line;
	}

	parseJSON("", str);

	while (m--) {
		cin >> line;
		if (!json.count(line)) {
			cout << "NOTEXIST" << endl;
		} else if (json[line] == "{}") {
			cout << "OBJECT" << endl;
		} else {
			cout << "STRING " << json[line] << endl;
		}
	}

	return 0;
}

201712-2 游戏
要细心,自己debug了十几分钟

#include <bits/stdc++.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

int n,k;
int a[1005]; 
int main()
{
    cin>>n>>k;
    int sum=n;
    int num=0;
    for(int i=1;i<=n;i++)	a[i]=1;
    int j=1;
    while(sum>1){
    	if(a[j]) num++;
    	else{
    		if(j>=n)	j=1;
    		else j++;
    		continue;
		}
    	if((num%k==0)||(num%10==k)){
     		a[j]=0;
    		sum--;
		}
		j++;
	}
	for(int i=0;i<=n;i++){
		if(a[i])	cout<<i;
	}
    return 0;
}

201712-3 Crontab
啊,写了好久,考虑前两种状况,按时间判断哪个能输出,结果只有5分,只过了一个样例。还超时了。
太麻烦了叭,选择看题解…(题解也老长了~)

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
 
typedef long long LL;
int firstYear, lastYear;	// 开始与结束的年份
int range[5] = { 60, 24, 31, 12, 7 };
int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
string week[] = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" },
mon[] = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" };
map<string, int> mp;	// 保存各个英文缩写对应的值
vector<pair<LL, int> > ans;	// ans存放结果的 "时间,id" 对
 
// 如果是字母开头,一定是英文缩写,直接返回mp中对应的值;否则就是数字,按位累加即可
int strToI(string &str)
{
	if (isalpha(str[0]))
		return mp[str];
	int num = 0;
	for (int i = 0; i != str.size(); ++i)
		num = num * 10 + str[i] - '0';
	return num;
}
 
void dealMinus(set<LL> &tmp, string &str)
{
	for (int i = 0; i != str.size(); ++i)
		str[i] = tolower(str[i]);	// 不区分大小写
	size_t tp = str.find('-');
	if (tp == string::npos)
		tmp.insert(strToI(str));
	else{
		string left = str.substr(0, tp), right = str.substr(tp + 1);
		int l = strToI(left), r = strToI(right);
		for (int i = l; i <= r; ++i)
			tmp.insert(i);
	}
}
 
// 先分析s是否为"*",再分析',',最后分析'-'
void findValue(set<LL> &tmp, string &s, int j)
{
	if (s == "*"){
		for (int i = 0; i != range[j]; ++i)
			if (j == 2 || j == 3)	// 日期和月份是1开头的
				tmp.insert(i + 1);
			else
				tmp.insert(i);
		return;
	}
	s += ',';
	size_t pos = 0, next = s.find(',');
	string str;
	while (next != string::npos){
		str = s.substr(pos, next - pos);
		dealMinus(tmp, str);
		pos = next + 1;
		next = s.find(',', pos);
	}
}
 
// 分析y年m月d日是星期几,顺便排除一下不合法的日期比如11月31日
int getDay(int year, int month, int day)
{
	days[2] = 28;
	if (year % 4 == 0)
		days[2] = 29;
	if (day > days[month])	// 该日期不存在
		return -1;
 
	int sum = 4, q = year - 1970;
	sum += q / 4 * 5 + q % 4;	// 每隔四年,sum应该+5
	if (q % 4 == 3)	// 这时说明 year-1 为闰年
		++sum;
	for (int i = 1; i != month; ++i)
		sum += days[i];
	sum += day - 1;
	return sum % 7;
}
 
// v[0] ~ v[3]不进行判断,只把tmp与pre中的内容组合起来,所有的判断放在v[4]中
void fillVector(vector<vector<LL> > &v, vector<LL> &tmp, int j, LL mul)
{
	if (j == 0)
		v[j] = tmp;
	else{
		vector<LL> &pre = v[j - 1];
		if (j != 4)
			for (int k = 0; k != tmp.size(); ++k)
				for (int l = 0; l != pre.size(); ++l)
					v[j].push_back(tmp[k] * mul + pre[l]);
 
		else{
			int judge[7] = { 0 };	// 用于判断一周中的每一天是否能够执行该命令
			for (int k = 0; k != tmp.size(); ++k)
				judge[tmp[k]] = 1;
			for (int l = 0; l != pre.size(); ++l){
				int month = pre[l] / 1000000, day = pre[l] / 10000 % 100;
				for (int year = firstYear; year <= lastYear; ++year){
					int dow = getDay(year, month, day);
					if (dow != -1 && judge[dow])	// 对每年的m月d日进行判断是否能够加入v[4]
						v[4].push_back(year * mul + pre[l]);
				}
			}
		}
	}
}
 
int main()
{
	for (int i = 0; i != 12; ++i)
		mp[mon[i]] = i + 1;
	for (int i = 0; i != 7; ++i)
		mp[week[i]] = i;
	int n;
	LL begin, end;
	cin >> n >> begin >> end;
	firstYear = begin / 100000000, lastYear = end / 100000000;
	vector<vector<string> > cron(n, vector<string>(6));	// 创建一个n*6的二维string数组cron
	for (int i = 0; i != n; ++i)
		for (int j = 0; j != 6; ++j)
			cin >> cron[i][j];
 
	// 对每一条指令,先分析有效的minute范围,存储在v[0],再分析有效的hour范围,将其与minute范围进行组合,存储在v[1]。
	// 以此类推,可以得出v[4]包含的即为最终的有效时间。
	for (int i = 0; i != n; ++i){
		vector<vector<LL> > v(5);
		LL mul = 1;
		for (int j = 0; j != 5; ++j){
			set<LL> valid;	// 因为题目的数据可能有 "3,2-4" 这种,所以用set存储去重复
			findValue(valid, cron[i][j], j);
			vector<LL> tmp;	// 为了方便使用之前版本的代码,这里新建一个vector保存valid中的数据
			for (set<LL>::iterator it = valid.begin(); it != valid.end(); ++it)
				tmp.push_back(*it);
			fillVector(v, tmp, j, mul);
			mul *= 100;
		}
		for (int j = 0; j != v[4].size(); ++j)
			ans.push_back(make_pair(v[4][j], i));
	}
 
	sort(ans.begin(), ans.end());
	for (vector<pair<LL, int> >::iterator it = ans.begin(); it != ans.end(); ++it)
		if (it->first >= begin && it->first < end)	// 不包含end
			cout << it->first << " " << cron[it->second][5] << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值