跳到正文

题解

题解:P12171 [蓝桥杯 2025 省 Python B] 最长字符串

Ashankamiko约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟
题目
P12171

版本

  1. v1当前

题目简述

题意

给出若干个字符串,按照以下要求输出字符串。

  • 输出的字符串必须是输入中的一个。
  • 长度必须是输入的字符串中最长的。
  • 如果长度相等,输出字典序最小的。

思路

总结以下经验。

  • 本题确实需要输入,不过是若干个,要用无限循环进行读入,并在读入字符串的时候根据上述要求模拟。
  • 最后输出满足上述要求的字符串即可。

AC 代码

C++
#include <bits/stdc++.h>
using namespace std;
#define in cin
#define out cout

int main() {
    string maxs = "", s;
    while (in >> s) { //若干组数据
        //对s处理
        if (s.size() > maxs.size()) //如果长度比maxs更长,直接更新maxs
            maxs = s;
        else if (s.size() == maxs.size() && s < mins) //否则比较长度,如果一样并且s的字典序比maxs的字典序更小,更新maxs
            maxs = s;
    }
    out << maxs; //输出答案
	return 0;
}
原文