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 = {p1, p2, ..., 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.
//一开始以为是到字符串题目,最后发现是规律题
题目只会出现6的字符(串):a,b,aa,bb,ab,ba.
然后存在一个合并规律:两个相同字符连在一起的时候可以合并成一个
那我们对每个字符串进行分析
1. aa/a bb/b
对这个组字符串来说,aa=a,bb=b,而任何一个单独的a,b都能与ab,ba进行合并,所以并不用太在意这四个
对于没有ab,ba的组合来说
他们的存在的可能值只可能是1(只存在a或只存在b) 2(a,b都存在)
2 ab/ba
这两组才是最会影响长度的因素
一组ab和ba会组成 aba字符串 也就是4个字符串会组成三个
在ab/ba数量不相等的时候,一定是minn=min(ba,ab),依靠这最小值进行组合(minn*2+1)
然后再去判断两个字符串数量的差值
多的串中,必然会与组串进行结合进行-1操作(开头结尾的字符相同)
然后剩下的字符串无法在进行合并操作,只能强项相连,每个串都会对答案+2.
所以可以得到规律
1.在没有ab/ba串的时候,如出现aa/a,bb/b中的一个,答案为1,若都出现为2
2.在ab/ba串有一个存在的时候,aa/b,b/bb可以与其合并,并不影响长度
所以答案就为max(ab,ba)*2
//无法进行这两个字符串的合并,只能选数量最多的串进行添加
3.在所有情况都出现的情况下
我们只用考虑ab和ba就好
求出min(ab,ba)*2+1 得到一个接近答案和合并串
然后利用ab/ba的差值加上2*abs(ba-ab)-1 即可得到答案
#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;
}
}
}