# CodeChef - MINSTR Minimize the string (规律题)

You are given n strings s1, s2, ..., sn. Each of these strings consists only of letters 'a' and 'b', and the length of each string can be at most 2. In other words, the only allowed strings are "a", "b", "aa", "ab", "ba" and "bb".

Consider a permutation p = {p1p2, ..., pn} of the integers {1, 2, ..., n}. Using this permutation, you can obtain a new string S = sp1 + sp2 + ... + spn, where the operator + denotes concatenation of strings.

You can shorten the string S by performing the following operation any number of times: choose two consecutive equal characters and remove one of these characters from the string. For example, the string "aabb" can be shortened to "abb" or "aab" in one operation, and then optionally it could still be shortened to "ab".

You are allowed to choose any permutation p. Take the string S obtained using this permutation, and using any sequence of operations, minimize the string length. Find the minimum possible length of the string obtainable.

### Input

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.

The first line of each test case contains an integer n.

The second line of each test case contains n space-separated strings s1, s2, ..., sn.

### Output

For each test case, output a single line containing one integer corresponding to the minimum possible length of the shortened string.

### Constraints

• 1 ≤ T ≤ 105
• 1 ≤ n ≤ 105
• sum of n over all test cases won't exceed 106

### Example

Input
2
2
ba ab
4
a b a b

Output
3
2


### Explanation

Testcase 1:

You can consider the permutation (2, 1). Using this, you get the string S = sp1 + sp2= ab + ba = abba. You can then take the two adjacent b's and remove one of them to get aba, whose length is 3. You cannot do any better, and hence the answer is 3.

Testcase 2:

You can consider the permutation (1, 3, 2, 4). Using this, you get the string S =sp1 + sp3 + sp2 + sp4 = a + a + b + b = aabb. You can then take the two adjacent b's and remove one of them to get aab. Then you can take the two adjacent a's and remove one of them to get ab. We end up with a length of 2, and you cannot do any better. Hence the answer is 2.

//一开始以为是到字符串题目，最后发现是规律题

1. aa/a bb/b

2 ab/ba

1.在没有ab/ba串的时候，如出现aa/a，bb/b中的一个，答案为1，若都出现为2

2.在ab/ba串有一个存在的时候，aa/b，b/bb可以与其合并，并不影响长度

//无法进行这两个字符串的合并，只能选数量最多的串进行添加

3.在所有情况都出现的情况下

#include<bits/stdc++.h>
using namespace std;

char x[10];
int cnt1,cnt2,cnt3,cnt4;

int main()
{
int t;
cin>>t;
while(t--)
{
cnt1=0;
cnt2=0;
cnt3=0;
cnt4=0;
int n;
cin>>n;
for(int i=0; i<n; i++)
{
scanf("%s",&x);
if(strcmp(x,"a")==0||strcmp(x,"aa")==0) cnt1++;
if(strcmp(x,"b")==0||strcmp(x,"bb")==0) cnt2++;
if(strcmp(x,"ab")==0) cnt3++;
if(strcmp(x,"ba")==0) cnt4++;
}
//printf("%d %d %d %d\n",cnt1,cnt2,cnt3,cnt4);
if(cnt4==0&&cnt3==0)
{
if(cnt1!=0&&cnt2!=0) cout<<2<<endl;
else cout<<1<<endl;
}
else if(cnt3==0||cnt4==0)
{
cout<<max(cnt3,cnt4)*2<<endl;
}
else
{
int k=abs(cnt4-cnt3);
//printf("%d\n",k);
int p=min(cnt4,cnt3);
int ans=2*p+1;
if(k!=0) ans+=2*k-1;
cout<<ans<<endl;
}
}
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120