K - Minimize the string

本文探讨了一种通过优化字符串操作来最小化最终字符串长度的算法。通过对一系列仅包含'a'和'b'的字符串进行排列组合,并允许删除相邻重复字符,算法寻找最短可能的字符串长度。案例分析展示了如何通过特定的字符串排列实现长度最小化。

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;
}

 

Write a C program to partitions a hypergraph G = (V, E) into 2 partitions. The Assignment Write a computer program that takes a netlist represented by a weighted hypergraph and partitions it into two partitions. Each node is associated with an area value and each edge has an edge cost. Your program should minimize the total cost of the cut set, while satisfying the area constraint that the total area of partition 1 should satisfy the balance criteria as described in the class. That is, if the area sum of all the nodes is A, then the area of partition 1 should be greater than or equal to ra-tio_factor *A – amax and less than or equal to ratio_factor *A + amax, where amax is the maximum value among all cell areas. The program should prompt the user for the value of ratio_factor. Assumptions and Requirements of the Implementation Your program should not have any limitation on the maximum number of nodes and the edges of the hypergraph. Each hyperedge could connect any subset of nodes in the hypergraph. Each node area is a non-negative integer, and each edge cost is a non-negative floating- point value. All the ids are 0-based. Namely, the id of the first element is 0, instead of 1. The output of each partition should include the list of node ids, sorted in the ascending order. The partition with the smaller minimum node id is listed first in the output. Use balance criteria as the tiebreaker when there are multiple cell moves giving the max-imum gain, as described in the class. Use the input and output formats given in the Sample Test Cases section. Sample Test Cases Test1: Please enter the number of nodes: 4 Please enter each of the 4 nodes with its id and the node area: 0 1 1 1 2 1 3 1 Please enter the number of edges: 3 Please enter each of the 3 edges with the number of connected nodes and their node ids, followed by the edge cost: 2 0 1 1 2 1 2 3 2 2 3 1 Please enter the percentage of the ratio factor: 50 The node ids of the partition 0 are 0 The node ids of the partition 1 are 1, 2, 3 The total cut cost is 1 Test2: Please enter the number of nodes: 4 Please enter each of the 4 nodes with its id and the node area: 0 1 1 4 2 2 3 1 Please enter the number of edges: 3 Please enter each of the 3 edges with the number of connected nodes and their node ids, followed by the edge cost: 3 0 1 2 5 3 0 2 3 3 3 0 1 3 4 Please enter the percentage of ratio factor: 50 The node ids of the partition 0 are 3 The node ids of the partition 1 are 0, 1, 2 The total cut cost is 7
07-08
10-02
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值