K - Minimize the string

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 1. 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. 2. Each node area is a non-negative integer, and each edge cost is a non-negative floating- point value. 3. All the ids are 0-based. Namely, the id of the first element is 0, instead of 1. 4. The output of each partition should include the list of node ids, sorted in the ascending order. 5. The partition with the smaller minimum node id is listed first in the output. 6. Use balance criteria as the tiebreaker when there are multiple cell moves giving the max-imum gain, as described in the class. 7. 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
//--------------------------------------------------------------------------- #ifndef MyTrayIconH #define MyTrayIconH //--------------------------------------------------------------------------- #include <SysUtils.hpp> #include <Classes.hpp> #include <System.hpp> #include <Controls.hpp> #include <Forms.hpp> #include <Extctrls.hpp> #include <ShellAPI.hpp> #include <StdLib.h> //--------------------------------------------------------------------------- #define WM_SYSTEM_MY_TRAY_NOTIFY (WM_USER + 1) namespace my_TrayIcon{ enum TMyTrayIconMessage {imClick, imDoubleClick, imMouseDown, imMouseUp, imLeftClickUp, imLeftDoubleClick, imRightClickUp, imRightDoubleClick, imNone}; } using namespace my_TrayIcon; class PACKAGE TMyTrayIcon : public TComponent { private: TNotifyIconDataW FData; bool FIsClicked; TIcon *FIcon; TImageList* FIconList; TPopupMenu* FPopupMenu; TTimer *FTimer; UnicodeString FHint; int FIconIndex; bool FVisible; bool FHide; bool FAnimate; TMyTrayIconMessage FAppRestore; TMyTrayIconMessage FPopupMenuShow; TWindowHook FApplicationHook; TNotifyEvent FOnMinimize; TNotifyEvent FOnRestore; TMouseMoveEvent FOnMouseMove; TMouseMoveEvent FOnMouseExit; TMouseMoveEvent FOnMouseEnter; TNotifyEvent FOnClick; TNotifyEvent FOnDblClick; TMouseEvent FOnMouseDown; TMouseEvent FOnMouseUp; TNotifyEvent FOnAnimate; TNotifyEvent FOnCreate; TNotifyEvent FOnDestroy; TNotifyEvent FOnActivate; TNotifyEvent FOnDeactivate; void __fastcall SetHint(String Hint); void __fastcall SetHide(bool Value); int __fastcall GetAnimateInterval(); void __fastcall SetAnimateInterval(int Value); bool __fastcall GetAnimate(); void __fastcall SetAnimate(bool Value); void __fastcall EndSession(); TShiftState ShiftState(); protected: virtual void __fastcall SetVisible(bool Value); virtual void __fastcall DoMessage(TMessage &Message); virtual void __fastcall DoClick(); virtual void __fastcall DoDblClick(); virtual void __fastcall DoMouseMove(TShiftState Shift, int X, int Y); virtual void __fastcall DoMouseDown(TMouseButton Button, TShiftState Shift, int X, int Y); virtual void __fastcall DoMouseUp(TMouseButton Button, TShiftState Shift, int X, int Y); virtual void __fastcall DoOnAnimate(TObject *Sender); virtual void __fastcall Notification(TComponent *AComponent, TOperation Operation); bool __fastcall ApplicationHookProc(TMessage &Message); virtual void __fastcall Loaded(); __property TNotifyIconDataW Data = {read=FData}; public: __fastcall TMyTrayIcon(TComponent* Owner); __fastcall ~TMyTrayIcon(); virtual void __fastcall Minimize(); virtual void __fastcall Restore(); virtual void __fastcall Update(); virtual void __fastcall ShowMenu(); virtual void __fastcall SetIconIndex(int Value); virtual void __fastcall SetDefaultIcon(); HWND __fastcall GetHandle(); __published: // Properties __property bool Visible = {read=FVisible,write=SetVisible,default=false}; __property UnicodeString Hint = {read=FHint,write=SetHint}; __property TPopupMenu* PopupMenu = {read=FPopupMenu,write=FPopupMenu}; __property bool Hide = {read=FHide,write=SetHide}; __property TMyTrayIconMessage RestoreOn = {read=FAppRestore,write=FAppRestore}; __property TMyTrayIconMessage PopupMenuOn = {read=FPopupMenuShow,write=FPopupMenuShow}; __property TImageList* Icons = {read=FIconList,write=FIconList}; __property int IconIndex = {read=FIconIndex,write=SetIconIndex,default=0}; __property int AnimateInterval = {read=GetAnimateInterval,write=SetAnimateInterval,default=1000}; __property bool Animate = {read=GetAnimate,write=SetAnimate,default=false}; __property bool Handle = {read=GetHandle}; // Events __property TNotifyEvent OnMinimize = {read=FOnMinimize,write=FOnMinimize}; __property TNotifyEvent OnRestore = {read=FOnRestore,write=FOnRestore}; __property TNotifyEvent OnClick = {read=FOnClick,write=FOnClick}; __property TMouseMoveEvent OnMouseEnter = {read=FOnMouseEnter,write=FOnMouseEnter}; __property TMouseMoveEvent OnMouseExit = {read=FOnMouseExit,write=FOnMouseExit}; __property TMouseMoveEvent OnMouseMove = {read=FOnMouseMove,write=FOnMouseMove}; __property TMouseEvent OnMouseUp = {read=FOnMouseUp,write=FOnMouseUp}; __property TMouseEvent OnMouseDown = {read=FOnMouseDown,write=FOnMouseDown}; __property TNotifyEvent OnAnimate = {read=FOnAnimate,write=FOnAnimate}; __property TNotifyEvent OnCreate = {read=FOnCreate,write=FOnCreate}; __property TNotifyEvent OnDestroy = {read=FOnDestroy,write=FOnDestroy}; __property TNotifyEvent OnActivate = {read=FOnActivate,write=FOnActivate}; __property TNotifyEvent OnDeactivate = {read=FOnDeactivate,write=FOnDeactivate}; }; //--------------------------------------------------------------------------- #endif 有什么问题
最新发布
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值