[Luogu && Atcoder] AT_code_thanks_festival_2018_d 题解
AT_code_thanks_festival_2018_d 题解
题意:给定一个字符串 \(s\),求最少要将 \(s\) 分成几个子串,才能使每个子串的开头的字符的字典序小于其余字符?
思路:定义一个变量 \(ans = 1\),为子串数,和一个字符变量 \(a=s_0\),遍历字符串,每当遍历到一个比 \(a\) 小或相等的字符,\(ans\) 加 \(1\),并将此字符赋值给 \(a\),然后继续遍历。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
string s;
ll ans = 1;
char a;
int main() {
cin >> s;
a = s[0];
for (ll i = 1; i < s.size(); ++i) {
if (s[i] <= a) {
++ans;
a = s[i];
}
}
cout << ans;
return 0;
}

浙公网安备 33010602011771号