练习codeforces1702

题目如下
B. Polycarp Writes a String from Memory
time limit per test2 seconds
memory limit per test256 megabytes
Polycarp has a poor memory. Each day he can remember no more than 3 of different letters.

Polycarp wants to write a non-empty string of 𝑠 consisting of lowercase Latin letters, taking minimum number of days. In how many days will he be able to do it?

Polycarp initially has an empty string and can only add characters to the end of that string.

For example, if Polycarp wants to write the string lollipops, he will do it in 2 days:

on the first day Polycarp will memorize the letters l, o, i and write lolli;
On the second day Polycarp will remember the letters p, o, s, add pops to the resulting line and get the line lollipops.
If Polycarp wants to write the string stringology, he will do it in 4 days:

in the first day will be written part str;
on day two will be written part ing;
on the third day, part of olog will be written;
on the fourth day, part of y will be written.
For a given string 𝑠, print the minimum number of days it will take Polycarp to write it.

Input
The first line of input data contains a single integer 𝑡 (1≤𝑡≤104) — the number of test cases.

Each test case consists of a non-empty string 𝑠 consisting of lowercase Latin letters (the length of the string 𝑠 does not exceed 2⋅105) — the string Polycarp wants to construct.

It is guaranteed that the sum of string lengths 𝑠 over all test cases does not exceed 2⋅105.

Output
For each test case, print a single number — minimum number of days it will take Polycarp to write the string 𝑠 from memory.

题目大意
规定主人公每天能记住三个字母,现有完整字符串,从前往后记忆,试问最短几天可以全部记住

题目分析
每天记忆三个字母同时考虑字符串的连续性,所以从前往后记忆,每当已经记忆的子字符串中的不同字符数达到上限,原有的记忆清空,天数加一,开始记忆第二天
注意最后的字符也要加进去

点击查看代码
for(int i = 0; i < len; i++){
            int ch = s[i] - 'a';
            if(find(memo.begin(), memo.end(), ch) != memo.end()){
                continue;
            }
            if(memo.size() == 3){
                days++;
                memo.clear();
            }
            memo.push_back(ch);
        }
最后输出记下这一整串需要几天

完整代码

点击查看代码
#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>

using namespace std;

int main(){
    int t;
    scanf("%d", &t);
    while (t--){
        char s[200005];
        scanf("%s", s);
        int len = strlen(s);
        vector<int> memo;
        int days = 1;
        for(int i = 0; i < len; i++){
            int ch = s[i] - 'a';
            if(find(memo.begin(), memo.end(), ch) != memo.end()){
                continue;
            }
            if(memo.size() == 3){
                days++;
                memo.clear();
            }
            memo.push_back(ch);
        }
        printf("%d\n", days);
    }
    return 0;
}

posted @ 2025-07-06 21:52  sirro1uta  阅读(8)  评论(0)    收藏  举报