K - 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 Sobtained 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.

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        char s[3];
        int a,b,c,d;
        a=b=c=d=0;
        for(int i=0; i<n; i++)
        {
            scanf("%s",s);
            if(strcmp(s,"a")==0||strcmp(s,"aa")==0)
                a++;
            else if(strcmp(s,"b")==0||strcmp(s,"bb")==0)
                b++;
            else if(strcmp(s,"ab")==0)
                c++;
            else d++;
        }
        int ans=0;
        if(b!=0)
            b=1;
        if(a!=0)
            a=1;
        if(d!=0||c!=0)
        {
            a=b=0;
           if(c>d)
            ans=c*2;
           else if(d>c)
            ans=d*2;
           else ans=d*2+1;
        }
        else ans=a+b;
        printf("%d\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值