CodeForces - 1430D String Deletion (思維)
You have a string ss consisting of nn characters. Each character is either 0 or 1.
You can perform operations on the string. Each operation consists of two steps:
- select an integer ii from 11 to the length of the string ss, then delete the character sisi (the string length gets reduced by 11, the indices of characters to the right of the deleted one also get reduced by 11);
- if the string ss is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix).
Note that both steps are mandatory in each operation, and their order cannot be changed.
For example, if you have a string s=s= 111010, the first operation can be one of the following:
- select i=1i=1: we'll get 111010 →→ 11010 →→ 010;
- select i=2i=2: we'll get 111010 →→ 11010 →→ 010;
- select i=3i=3: we'll get 111010 →→ 11010 →→ 010;
- select i=4i=4: we'll get 111010 →→ 11110 →→ 0;
- select i=5i=5: we'll get 111010 →→ 11100 →→ 00;
- select i=6i=6: we'll get 111010 →→ 11101 →→ 01.
You finish performing operations when the string ss becomes empty. What is the maximum number of operations you can perform?
Input
The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases.
The first line of each test case contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of the string ss.
The second line contains string ss of nn characters. Each character is either 0 or 1.
It's guaranteed that the total sum of nn over test cases doesn't exceed 2⋅1052⋅105.
Output
For each test case, print a single integer — the maximum number of operations you can perform.
Example
Input
5
6
111010
1
0
1
1
2
11
6
101010
Output
3
1
1
1
3
Note
In the first test case, you can, for example, select i=2i=2 and get string 010 after the first operation. After that, you can select i=3i=3 and get string 1. Finally, you can only select i=1i=1 and get empty string.
題意:
給出一個01串,每步操作包括兩小步,一是從字串中選擇一個字元刪掉,二是把字首相同的字元全都刪掉(這兩小步算一步),問最多需要幾步把字串刪成空串
思路:
問最多的步數,也就是要多做無用功,在單獨刪除字元時儘量刪相同字元長度>1的段中的字元。首先計算出每個連續相同字元段的長度,考慮把每個段的長度看作1,多餘的可以單獨刪除字元時貢獻無用功,所以只需要從前向後掃的過程中記錄後面多餘字元的個數,
(1)如果某段長度==1,從多餘字元中先刪掉一個,再把該段當作字首刪掉;如果此時沒有多餘字元了,也就是說後面的段長都為1,那麼之後就兩兩一刪就行了
(2)如果某段長度>1,多餘字元要減掉該段貢獻的多餘字元數,
最後可以發現,如果有足夠多的多餘字元,答案就是連續相同字元段的個數,否則,刪第 i 段時沒有多餘字元了答案就是i - 1 + (tot - i + 2) / 2,tot是連續相同字元段的個數
相關文章
- 【CodeForces】 Codeforces Round #672 (Div. 2) B.Rock and Lever (思維&位運算)
- C. Lose it!(思維)Codeforces Round #565 (Div. 3)
- C. Dominant Piranha(思維) Codeforces Round #677 (Div. 3)NaN
- 計算機程式的思維邏輯 (29) – 剖析String計算機
- Codeforces A. Add Candies (#683 Div.2 by Meet IT) (思維 / 水題)
- Codeforces Round #688 (Div. 2) C. Triangles(思維,數學)
- Codeforces Round 944 (Div. 4) G(思維 + 位運算性質)
- Paimon Deletion VectorAI
- 思維體系---技術思維、業務資料思維、產品思維、複合思維
- 淺析工具思維、產品思維、品牌思維與定位
- 求職思維和招聘思維求職
- Codeforces 954I Yet Another String Matching Problem
- 把流量思維變成留量思維
- 思維鏈?思維樹?華為諾亞:現在到了思維森林時刻!
- 框架思維框架
- 極思維
- 黑客思維黑客
- acm-(思維、奇偶性、矩陣)Codeforces Round #682 (Div. 2) C. Engineer ArtemACM矩陣
- SAP PM 入門系列18 - IP25為維護計劃 Set Deletion Flag
- 你是整體思維還是分析思維? - kentbeck
- 計算思維
- 模型思維(01)模型
- Codeforces #698 (Div. 2) E. Nezzar and Binary String 題解
- 提升思維邏輯—SimpleMind Pro(思維導圖) for Mac/winMac
- 《計算思維史話》思維導圖——持續更新
- 創新思維框架:第一原則思維 - Neil Kakkar框架
- 什麼是產品思維和專案思維? - Shreyas
- 英語思維與物件導向分析思維的關係物件
- 運維工程師思維導圖運維工程師
- Java思維理清思路Java
- HDU – 4811 – Ball (思維)
- typora思維導圖
- 技術思維2
- 前端思維導圖前端
- 程式碼之外系列第一:索證思維與索進思維
- 21 Educational Codeforces Round 136 (Rated for Div. 2)Knowledge Cards(樹狀陣列、set、+思維、數字華容道)陣列
- Xmind使用教程:給思維導圖加水印 「Xmind思維導圖2023」
- 思維決定命運,從四個方面理解深度思維的概念