「果壳杯」 ROUND 10 (Div. 4) 题解

YIZHIYANG初来乍到 2026-11-26 15:42:46 12 浏览 0 点赞 0 收藏

本帖汇总本场比赛各题的解法与参考程序。

比赛入口

  • A. 遥遥领先
  • B. 公共子序列
  • C. AI助教系统
  • D. 核桃数
  • E. 手机反省

1. 遥遥领先

思路来源

  • 题目实质:在一段最长 1010 万字符的文本中,统计大小写不敏感、前后不接字母或数字的独立单词 “YYLX” 出现次数。

  • 经典做法:线性扫描 + 有限状态机 / 正则。这里直接用字符遍历即可 O(N)O(N)。

需要维护的信息

  1. 当前字符是否属于“字母或数字”。

  2. 一个滑动窗口保存最近 44 个字符,转成小写后与 “yylx” 比较。

  3. 窗口左右两侧相邻字符是否为 [A-Za-z0-9]。

细节点 & 坑

  • 文本中包含回车、空格、制表、标点,都算分隔符。

  • “跨行拼接”的 yyl\nx 不算,因为\n非字母数字,会把单词断开。

  • 末尾 # 仅作终止符,不参与计数。

代码

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

int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    string s, all;
    while (getline(cin, s)) 
    {
        if (!s.empty() && s.back() == '#') 
        {          // 终止行
            all += s.substr(0, s.size() - 1);
            break;
        }
        all += s + '\n';                              // 保留换行
    }

    auto isAlnum = [](char c) { return isalpha(c) || isdigit(c); };

    int cnt = 0, n = all.size();
    for (int i = 0; i + 3 < n; ++i) 
    {
        if (tolower(all[i])   == 'y' &&
            tolower(all[i+1]) == 'y' &&
            tolower(all[i+2]) == 'l' &&
            tolower(all[i+3]) == 'x') {
            bool left  = (i-1 >= 0 && isAlnum(all[i-1]));
            bool right = (i+4 < n && isAlnum(all[i+4]));
            if (!left && !right) ++cnt;
        }
    }
    cout << cnt << '\n';
    return 0;
}

2. 公共子序列答案判定

思路来源

  • 判定一个字符串 ans 是否既是 X 与 Y 的公共子序列,且长度等于给定 l。

  • 公共子序列判定 ➜ 双指针扫描 O(∣X∣+∣Y∣+∣ans∣)O(|X|+|Y|+|ans|)。

需要维护的信息

  1. 两个指针 i, j 走 X, Y。

  2. 扫描 ans 时分别在 X,Y 找下一个匹配字符。

细节点 & 坑

  • 不必验证老师给的 l 真的是 LCS 长度,只需比较学生答案长度是否等于 l。

  • 字符串允许到1000 1000,单次扫描完全足够。

  • 学生提交串可能 比 l 长、包含非字母 等,这些都要判错。

代码

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

bool check(string s, string t) 
{     // s 是否为 t 的子序列
    size_t i = 0;
    for (char c : t) if (i < s.size() && s[i] == c) ++i;
    return i == s.size();
}

int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, l;  cin >> n >> l;
    string X, Y;
    cin >> X >> Y;
    string ans;
    while (n--) 
    {
        cin >> ans;
        if (int(ans.size()) == l && check(ans, X) && check(ans, Y))
            cout << "yes\n";
        else
            cout << "no\n";
    }
    return 0;
}

3. AI 助教 Token 统计

思路来源

  • 模拟整场对话,每轮 问→答:

    • 提问行的 token 暂存;只有等到对应 AI 回答出现时,这部分 token 才并入“输入”计数。

    • 回答行的 token 即刻加入“输出”计数。

  • Token 定义:连写英文字母视作一个 token;单独标点视作一个 token;空格忽略。

细节点 & 坑

  • 数据保证“问答交替”,无需额外配对判断。

  • 多空格、标点相邻等情况要正确分割。

  • 如果对话以 学生行结束(没等到回答),最后那句不算输入。

代码

#include <bits/stdc++.h>
#define int long long
using namespace std;

int cntTok(string s){          // 统计一行 Token 数
	int n = s.size(), t = 0;
	for(int i = 0; i < n; ){
		if(isalpha(s[i])){
			++ t;
			while(i < n && isalpha(s[i])) ++ i;
		}else if(s[i] == ' ') ++ i;
		else { ++ t; ++ i; }
	}
	return t;
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	string s;
	int p = 0;                
	int in = 0, out = 0;     // 结果
	vector<string> words;
	while(getline(cin ,s), s.find("#") == -1)	
    words.push_back(s);
	
	for(int i = 0;i < words.size(); ++ i)
	{
		string l = words[i];
		if(l.size() >= 2 && l[0] == '#' && l[1] == ':') break;
		char r = l[0];                // 角色
		int tok = cntTok(l.substr(2));
		p += tok;// 前缀和
		if(r == 'S') 
		{
			if(i + 1 != words.size()) in += p;  
		}
		else
		{
		    out += tok;
		}
	}
	cout << in << ' ' << out << '\n';
return 0;
}


4. 核桃数判定

题目太水了,不想写思路了,进制转换判断是否出现过单独的一段000 或者111即可,直接放个AC代码

代码

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
signed main()
{
    int n;
    for(cin >> n; n --;)
    {
        unsigned int x;
        cin >> x;
        int p = x & 1, c = 1;
        bool ok = 0;
        x >>= 1;
        while(x)
        {
            int b = x & 1;
            if(b == p) c ++;
            else
            {
                if(c == 3) { ok = 1; break; }
                c = 1, p = b;
            }
            x>>=1;
        }
        if(!ok && c == 3) ok = 1;
        cout << (ok ? "yes" : "no") << endl;
    }
    return 0;
}

5. 手机反省(熬夜统计)

思路来源

题目要求 求熬夜次数 和 哪些app启动次数最多

每条记录所属的“熬夜窗口”定义:

  • 若 h ≥ 23 &&(m || s) ● 则窗口起点是后一天,编号用 day = d +1。
  • 否则 (h < 3) ● 窗口属于 当天 的23:00:00 23:00:00 → day = d。
  • 其余时间不计。

需要维护的信息

  • night: 标记夜晚是否出现至少一次记录。

  • 两级统计:夜晚维度、appapp 维度。

  • 所以开两个桶,维护夜晚 和app app的信息

用

  • night[day]:标记当天有没有熬夜;

  • app[id]:该 appapp 在所有熬夜夜晚出现次数。

细节点 & 坑

  • 天编号可能出现 000,365,要允许 day+1 =366 的凌晨
  • 输出并列 app 升序。
  • 非熬夜记录完全忽略。

代码

#include <bits/stdc++.h>
using namespace std;
int night[1001],app[1001];

int main()
{
	int n;
	cin >> n;
	while(n --)
  {
		int id,day,h,m,s;char ch;
		cin >> id >> day >> ch >> h >> ch >> m >> ch >> s;
		if(h == 23 && (m || s) || h < 3)
    {
			++ app[id];
      if(h == 23) 
        night[day + 1] = 1;
      else
        night[day] = 1;
		}
	}
	
	int ans = 0;
	for(int i = 0;i <= 366;++ i)
  {
		if(night[i]) ans ++;
	}
	
	cout << ans << "\n";
	
	if(ans == 0)
  {
		cout << "#";
		return 0;
	}
	int mx = 0;
	for(int i = 0;i <= 999;++ i)
  {
		mx = max(mx,app[i]);
	}
	
	for(int i = 0;i <= 999;++ i)
  {
		if(app[i] == mx)
    {
			cout << setw(3) << setfill('0') << i << " ";
		}
	}
	
	return 0;
}

总结

一车的字符串的题,可以审视编码能力还不是很过关,菜就多练!!!

评论

0 条
还没有评论。