- 输入多组扩号字符串,自由排列,尽可能的让匹配的对数多。
- 先对单个字符串处理,把单一字符串中能匹配的括号配对出来。
- 队列按单边括号最少数排列(字符串连接配对的括号对数多)
- 把处理好的字符串入队
- 取出前两个字符串,按各自的左,右括号数确定,各自放在左还是右
- 再把5中的处理出的新字符串入队(没有就忽略)
- 直到无法再匹配,结束输出答案
代码:
#include
#include
#include
#include
#include
#include
using namespace std;
char s[100005];
struct node
{
int st;
int et;
bool operator <(const node a)const
{
return min(st,et) q;
stack c;
int main()
{
int t,n,a,b,st,et,sum;
scanf("%d",&t);
while(t--)
{
while(!q.empty()) q.pop();
sum=0;
scanf("%d",&n);
for(int i=0;imin(now1.et,now2.st))
{
swap(now1.st,now2.st);
swap(now1.et,now2.et);
}
sum+=min(now1.et,now2.st);
node ti;
if(now1.et>now2.st)
{
ti.et=now1.et-now2.st+now2.et;
ti.st=now1.st;
}
else
{
ti.st=now2.st-now1.et+now1.st;
ti.et=now2.et;
}
if(ti.et||ti.st)
q.push(ti);
}
printf("%d\n",sum*2);
}
}