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是連續相同字元段的個數
相關文章
- C. Lose it!(思維)Codeforces Round #565 (Div. 3)
- 【CodeForces】 Codeforces Round #672 (Div. 2) B.Rock and Lever (思維&位運算)
- 思維體系---技術思維、業務資料思維、產品思維、複合思維
- 計算機程式的思維邏輯 (29) – 剖析String計算機
- 計算機程式的思維邏輯 (29) - 剖析String計算機
- 求職思維和招聘思維求職
- Codeforces Round #688 (Div. 2) C. Triangles(思維,數學)
- Codeforces A. Add Candies (#683 Div.2 by Meet IT) (思維 / 水題)
- C. Dominant Piranha(思維) Codeforces Round #677 (Div. 3)NaN
- Codeforces Round 944 (Div. 4) G(思維 + 位運算性質)
- 淺析工具思維、產品思維、品牌思維與定位
- Codeforces #208 div2前兩題及思維風暴
- 黑客思維黑客
- 思維模式模式
- 框架思維框架
- 組合思維與繼承思維的不同繼承
- 模型思維(01)模型
- 計算思維
- Codeforces Round #225 (Div. 2)(B思維題,E:dfs+樹狀陣列)陣列
- 《計算思維史話》思維導圖——持續更新
- 創新思維框架:第一原則思維 - Neil Kakkar框架
- 你是整體思維還是分析思維? - kentbeck
- 提升思維邏輯—SimpleMind Pro(思維導圖) for Mac/winMac
- 什麼是產品思維和專案思維? - Shreyas
- acm-(思維、奇偶性、矩陣)Codeforces Round #682 (Div. 2) C. Engineer ArtemACM矩陣
- 英語思維與物件導向分析思維的關係物件
- 思維之道 --讀《質量.軟體.管理 系統思維》(2)
- 思維之道 --讀《質量.軟體.管理 系統思維》(1)
- 思維之道 --讀《質量.軟體.管理 系統思維》(3)
- 前端思維導圖前端
- Java思維理清思路Java
- 結構化思維
- 原型設計思維原型
- 新思路,新思維
- 談談“模式思維”模式
- 技術思維2
- typora思維導圖
- SAP PM 入門系列18 - IP25為維護計劃 Set Deletion Flag