【CF1774C】Ice and Fire (Construction, DP)

[Title description]
There are nnn person, theiii person's temperature isiii .
Environment type is0 00 or1 11 , if the environment is0 00 , the person with the lower temperature wins, if the condition is1 11 , the person with the highest temperature wins,n − 1 n-1n1 environment type forms a lengthn − 1 n-1nThe binary string ssof 1s .
Ifxxx people participate in the game, then there arex − 1 x-1x1 battle, environment type isssthe first x of s − 1 x-1x1 element. where there are not less than2 2When there are 2 people, choose2 22 people fight, of whichiiThe environment type of the i battle issi s_isi.
For any one from 2 to 22 tonnn ofxxx , if all temperatures do not exceedxxAll x people participate in the competition, how many people have a chance to win (survive to the end)?

[Input format]
An integer t in the first line ( 1 ≤ t ≤ 1 0 3 ) t(1\le t\le 10^3)t(1t103 ), indicating the number of test samples. Enter an integer n ( 2 ≤ n ≤ 2 × 1 0 5 ) n(2\le n\le 2\times 10^5)
for the first line of each test samplen(2n2×105 ), indicating the number of players.
The second line enters a lengthn − 1 n-1n1 for the stringsss . Data guarantees nn
in each set of test casesThe sum of n does not exceed3 × 1 0 5 3\times 10^53×105

[Output format]
For each set of test cases, output a line with a total of n − 1 n-1n1 integer, i.e. for 2 from22 tonneveryxx of nx , how many people have a chance to win.

【Input sample】

2
4
001
4
101

【Example of output】

1 1 3
1 2 3

【Explanation/Tip】
In the first test case, for x = 2 x=2x=2 and x = 3 x=3 x=3, only the player whose temperature value is 1 1 1 can be the winner. For x = 4 x=4 x=4 , the player whose temperature value is 2 , 3 , 4 2,3,4 2,3,4 can be the winner.

【analyze】


Suppose the environment of the last game is 1 11 , that is, the person with the highest temperature wins, then the temperature is1 1People with 1 will lose even if they live to the last game.

Similarly, if the last kkThe environment of field k is1 11 , then there arek ( 1 ∼ k ) k(1\sim k)k(1k ) Individuals must have no chance to win, the otherx − k xkxAll k players have a chance to win, because only the temperature is greater than or equal tok + 1 k + 1k+1 person may continuously beat the temperature of1 ∼ k 1\sim k1k 's people live to the end.

So how to ensure that k + 1 ∼ x k+1\sim xk+1x contestants have a chance to live to the endkkWhat about k field? Suppose you want to make playeri ( k + 1 ≤ i ≤ x ) i(k+1\le i\le x)i(k+1ix ) live to the endkkk field, just let the rest be ink + 1 ∼ x k+1\sim xk+1The players in x play against each other, since the k − 1 k-1k1 field environment must be0 00 , then let the remaining playerjjj and1 ∼ k 1\sim k1Players in k play and are eliminated, then iii lived till the endkkk field.

Conversely, if the last kkThe environment of field k is all0 00 can also prove that the number of people who have a chance to win isx − k xkxk


【Code】

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int main()
{
    
    
    int T, n;
    string s;
    cin >> T;
    while (T--)
    {
    
    
        cin >> n >> s;
        cout << 1 << ' ';
        for (int i = 1, k = 1; i < s.size(); i++)  // k表示最长相同后缀的长度
        {
    
    
            if (s[i] == s[i - 1]) k++;
            else k = 1;
            cout << i + 2 - k << ' ';  // 第1 ~ i + 2名选手参赛
        }
        cout << endl;
    }
    return 0;
}

おすすめ

転載: blog.csdn.net/m0_51755720/article/details/130052276