CodeChef - MINSTR Minimize the string (规律题)

原创 2018年04月16日 19:34:14

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.


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

题目只会出现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;
        }
    }
}



CodeChef March Challenge 2017 题解

CC XENTASK由于要交替选,所以要不第一个人选位置为奇数的数,第二个人选位置为偶数的数,要不第一个人选位置为偶数的数,第二个人选位置为奇数的数。取最小值即可。#include #include...
  • YxuanwKeith
  • YxuanwKeith
  • 2017-03-13 20:26:30
  • 643

Codechef 2017 March Challenge 简要题解

在比赛进行到一半的时候才参加,,导致challenge没法搞得很好       写一个简要题解吧,,challenge太差了就不放了 Xenny and Alternating Tasks:直接按照题...
  • lych_cys
  • lych_cys
  • 2017-03-13 21:01:57
  • 817

Codechef Nuclear Reactors 题解

There are K nuclear reactor chambers labelled from 0 to K-1. Particles are bombarded onto chamber 0....
  • kenden23
  • kenden23
  • 2014-05-04 07:01:38
  • 785

codechef September Challenge 2017解题报告

前记: 第一次打cc感觉要自爆啊。 (不过似乎题目非常良心啊,似乎没有撕烤题啊) Little Chef and Sums 第一个最小值的位置。 #include &...
  • wzf_2000
  • wzf_2000
  • 2017-09-27 16:23:43
  • 249

Codechef A temple of Snakes(思维)

点击打开链接  题意 你想要为众蛇建造一座神殿。神殿将建在群山之中,可以将其视作n个块,其中第i个的高度为hi。神殿会建造在连续若干个块上,要求这些块的高度从1开始逐渐增加到某个高度,...
  • CillyB
  • CillyB
  • 2017-05-30 15:21:20
  • 245

[多维FFT Bluestein′s Algorithm] Codechef October Challenge 2017 .Chef and Horcrux

题目里那个 其实不重要,只要能算出 pip_i 就行了pip_i 的话发现就是个多维FFT的转移形式到Hillan大佬博客里拷了个代码改一改就好了…Bluestein算法里的FFT可以用暴力卷积代替...
  • Coldef
  • Coldef
  • 2017-10-17 07:47:48
  • 291

CodeChef:Girl Friend and String Gift(dp)

Chef's Girl Friend has given him a unique gift. She has given him a string S. Chef being a gentleman...
  • junior19
  • junior19
  • 2017-09-15 22:03:17
  • 47

面试编程题:(一)数字规律

面试编程题之一组有规律的数,找出其中的某个或某些数。
  • zxx910509
  • zxx910509
  • 2016-10-03 21:30:26
  • 582

hdu acm中规律题总结

1、母牛的故事         a[0]=0;a[
  • ycy0706
  • ycy0706
  • 2014-08-14 17:23:18
  • 366

【笔试/面试】—— 数学找规律题

所谓找规律其实是寻找一个函数 f(x)f(x),一个映射。 题目 1:7 or 22已知: 3 + 4 ⇒ 19, 5 + 6 ⇒ 41, 问 1 + 3 ⇒ ?题目的解不唯一: (1) 解法 ...
  • lanchunhui
  • lanchunhui
  • 2016-04-30 08:24:59
  • 2120
收藏助手
不良信息举报
您举报文章:CodeChef - MINSTR Minimize the string (规律题)
举报原因:
原因补充:

(最多只允许输入30个字)