CodeForces - 1430D String Deletion (思維)

2018_XWJ發表於2020-10-13

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:

  1. 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);
  2. 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:

  1. select i=1i=1: we'll get 111010 →→ 11010 →→ 010;
  2. select i=2i=2: we'll get 111010 →→ 11010 →→ 010;
  3. select i=3i=3: we'll get 111010 →→ 11010 →→ 010;
  4. select i=4i=4: we'll get 111010 →→ 11110 →→ 0;
  5. select i=5i=5: we'll get 111010 →→ 11100 →→ 00;
  6. 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是連續相同字元段的個數

 

相關文章