1 条题解

  • 0
    @ 2025-8-24 22:39:40

    自动搬运

    查看原文

    来自洛谷,原作者为

    avatar 5ab_juruo
    May you find some comfort here

    搬运于2025-08-24 22:39:40,当前版本为作者最后更新于2022-07-20 12:59:00,作者可能在搬运后再次修改,您可在原文处查看最新版

    自动搬运只会搬运当前题目点赞数最高的题解,您可前往洛谷题解查看更多

    以下是正文


    简单模拟题,送分。

    subtask1

    没有空白字符,即文本中没有空格和换行。

    根据题目定义,一个标题至少要有一个空格,所以答案总是 00

    subtask2

    只有一行,答案要么是 00 要么是 11,随机一个即可。

    一个满足要求的标题总是由如下要素构成:

    • (可选)井号前的空格;

    • (必须)恰好一个井号;

    • (必须)至少一个空格;

    • (必须)至少一个非空白字符;

    • 后面的随意。

    按照顺序一个一个检查就可以了。还有一个问题在于,如何处理换行。

    一个方法是在输入整数后读入一行。也可以使用 scanf("%d\n")。 某些快读可能能自动过滤后面一个字符,这也能解决这个问题。

    以及,不推荐使用 gets(),这东西在 C++14 被橄榄了,到赛场上会 CE。

    subtask3

    把子任务 2 里面的方法每行做一遍就好了。

    /* name: a
     * author: 5ab
     * created at: 22-07-24 19:52
     */
    #include <iostream>
    #include <string>
    using namespace std;
    
    typedef long long ll;
    
    signed main()
    {
    	ios_base::sync_with_stdio(false);
    	cin.tie(nullptr);
    	
    	int n, ans = 0;
    	string s;
    	
    	cin >> n, getline(cin, s);
    	for (int i = 0, j; i < n; i++)
    	{
    		getline(cin, s);
    		
    		j = 0;
    		while (j < s.length() && s[j] == ' ')
    			j++;
    		if (j >= s.length() - 1 || s[j] != '#' || s[++j] != ' ')
    			continue;
    		while (j < s.length() && s[j] == ' ')
    			j++;
    		if (j >= s.length())
    			continue;
    		ans++;
    	}
    	
    	cout << ans;
    	
    	return 0;
    }
    

    这道题也可以用正则表达式做,把标题的正则表达式一个一个写出来就可以了。

    /* name: a
     * author: 5ab
     * created at: 22-07-24 19:52
     */
    #include <iostream>
    #include <string>
    #include <regex>
    using namespace std;
    
    typedef long long ll;
    
    signed main()
    {
    	ios_base::sync_with_stdio(false);
    	cin.tie(nullptr);
    	
    	int n, ans = 0;
    	string s;
    	regex pattern(" *# +[a-z#].*");
    	
    	cin >> n, getline(cin, s);
    	for (int i = 0, j; i < n; i++)
    	{
    		getline(cin, s);
    		if (regex_match(s, pattern))
    			ans++;
    	}
    	
    	cout << ans;
    	
    	return 0;
    }
    
    • 1

    信息

    ID
    7818
    时间
    1000ms
    内存
    512MiB
    难度
    1
    标签
    递交数
    0
    已通过
    0
    上传者