NAIPC 2017

Yin and Yang Stones

  • 75.39%
  • 1000ms
  • 262144K
 

A mysterious circular arrangement of black stones and white stones has appeared. Ming has been tasked with balancing the stones so that only one black and one white stone remain.

Ming has two operations for balancing the stones:

  1. Take some consecutive sequence of stones where there is exactly one more black stone than a white stone and replace the stones with a single black stone
  1. Take some consecutive sequence of stones where there is exactly one more white stone than black stone and replace the stones with a single white stone

Given a circular arrangement, determine if it is possible for Ming to balance the stones.

Input

Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. The input will consist of a single string sss (1≤∣s∣≤105)(1 \le |s| \le 10^5)(1≤∣s∣≤105), with only the characters capital ‘BBB’ and ‘WWW’. The stones are arranged in a circle, so the first stone and the last stone are adjacent.

Output

Output 111 if it is possible for Ming to balance the stones with his rules. Otherwise, output 000.

样例输入1

WWBWBB

样例输出1

1

样例输入2

WWWWBBW

样例输出2

0

样例输入3

WBBBBBWWBW

样例输出3

0

题目来源

The North American Invitational Programming Contest 2017

思路:W与B相同输出1,否则输出0。只有这样才能保持黑白平衡。

Pieces of Parentheses

  • 22.03%
  • 1000ms
  • 262144K
 

You are teaching a class in programming, and you want to cover balanced parentheses. You’ve got a great visual aid, a sign with a very long, balanced string of parentheses. But, alas, somehow, your visual aid has been broken into pieces, and some pieces may be missing! You’ve got to try to put it back together as best you can. Given the string of parentheses on each piece, what is the longest balanced string you can form by concatenating some of them in some order? Each piece may be used at most once, and the pieces cannot be reversed.

A balanced string of parentheses is defined as:

  1. The empty string
  1. ABABAB where AAA and BBB are both balanced strings of parentheses
  1. (A)(A)(A) where AAA is a balanced string of parentheses

Input

Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. The first line of input will contain a single integer n(1≤n≤300)n (1 \le n \le 300)n(1≤n≤300), which is the number of pieces.

Each of the next nnn lines will hold a single string s(1≤∣s∣≤300)s (1 \le |s| \le 300)s(1≤∣s∣≤300), which consists only of the characters ’(((’ and ’)))’. This describes one of the pieces.

Output

Output a single integer, which is the length of the longest string of balanced parentheses you can form from the pieces. Note that the empty string is technically a balanced string of parentheses, so it is always possible to form a string of length at least 000 (although the empty string is not a very effective visual aid!).

样例输入1

3
())
((()
)()

样例输出1

10

样例输入2

5
)))))
)
((
))((
(

样例输出2

2

题目来源

The North American Invitational Programming Contest 2017

代码:

 #include <iostream>
#include <cstring>
#include <queue>
using namespace std;
template <class T, class C>
using heap = priority_queue<T, vector<T>, C>;
void abc(string s, int &a, int &b, int &c)
{
a = ,
b = ,
c = s.length();
for (int i = ; i < s.length(); i++)
{
switch (s[i])
{
case '(':
a++;
break;
case ')':
if (a > )
{
a--;
}
else
{
b++;
}
}
}
}
struct triple
{
int a,
b,
c;
};
bool operator>(const triple &A, const triple &B)
{
if (A.b ^ B.b)
{
return A.b > B.b;
}
if (A.a ^ B.a)
{
return A.a < B.a;
}
return A.c < B.c;
}
bool operator<(const triple &A, const triple &B)
{
if (A.a ^ B.a)
{
return A.a > B.a;
}
if (A.b ^ B.b)
{
return A.b < B.b;
}
return A.c < B.c;
}
int main()
{
int n{};
cin >> n;
int A[], B[];
memset(A, 0xf0, sizeof(A));
memset(B, 0xf0, sizeof(B));
A[] = ;
B[] = ;
heap<triple, greater<triple>> I;
heap<triple, less<triple>> D;
for (int i = ; i <= n; i++)
{
string s;
cin >> s;
int a{}, b{}, c{};
abc(s, a, b, c);
if (a >= b)
{
I.push({a, b, c});
}
else
{
D.push({a, b, c});
}
}
while (I.size())
{
const int a = I.top().a,
b = I.top().b,
c = I.top().c;
for (int x = ; x >= max(b, a - b); x--)
{
A[x] = max(A[x], A[x - a + b] + c);
}
I.pop();
}
while (D.size())
{
const int a = D.top().a,
b = D.top().b,
c = D.top().c;
for (int x = ; x >= max(a, b - a); x--)
{
B[x] = max(B[x], B[x - b + a] + c);
}
D.pop();
}
int reponse{};
for (int x = ; x <= ; x++)
{
reponse = max(reponse, A[x] + B[x]);
}
cout << reponse << endl;
return ;
}

参考博客:http://www.cnblogs.com/JebediahKerman/p/9742462.html

The North American Invitational Programming Contest 2017 题目的更多相关文章

  1. The North American Invitational Programming Contest 2018 D. Missing Gnomes

    A family of nn gnomes likes to line up for a group picture. Each gnome can be uniquely identified by ...

  2. The North American Invitational Programming Contest 2018 H. Recovery

    Consider an n \times mn×m matrix of ones and zeros. For example, this 4 \times 44×4: \displaystyle \ ...

  3. The North American Invitational Programming Contest 2018 E. Prefix Free Code

    Consider nn initial strings of lower case letters, where no initial string is a prefix of any other ...

  4. North American Invitational Programming Contest (NAIPC) 2017

    (待补) A. Pieces of Parentheses 将括号处理完成后排序,方式参加下面的博客.然后做一遍背包即可. 2018 Multi-University Training Contest ...

  5. North American Invitational Programming Contest (NAIPC) 2016

    (待补) A. Fancy Antiques 爆搜. B. Alternative Bracket Notation C. Greetings! D. Programming Team 0/1分数规划 ...

  6. North American Invitational Programming Contest 2018

    A. Cut it Out! 枚举第一刀,那么之后每切一刀都会将原问题划分成两个子问题. 考虑DP,设$f[l][r]$表示$l$点顺时针一直到$r$点还未切割的最小代价,预处理出每条边的代价转移即可 ...

  7. 2014 ACM-ICPC Beijing Invitational Programming Contest

    点击打开链接 Happy Reversal Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      J ...

  8. ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2017)- K. Poor Ramzi -dp+记忆化搜索

    ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2017)- K. ...

  9. 训练20191007 2017-2018 ACM-ICPC Latin American Regional Programming Contest

    2017-2018 ACM-ICPC Latin American Regional Programming Contest 试题地址:http://codeforces.com/gym/101889 ...

随机推荐

  1. SparkSQL执行时参数优化

    近期接手了不少大数据表任务调度补数据的工作,补数时发现资源消耗异常的大且运行速度却不怎么给力. 发现根本原因在于sparkSQL配置有诸多问题,解决后总结出来就当抛砖引玉了. 具体现象 内存CPU比例 ...

  2. WPF的IsSynchronizedWithCurrentItem属性

    如果两个控件都绑定到同一个源(ObservableCollection)集合视图时,该对象会自动绑定到该视图的 CurrentItem.请注意,CollectionViewSource 对象会自动同步 ...

  3. 20155236范晨歌 Exp7 网络欺诈技术防范

    20155236范晨歌 Exp7 网络欺诈技术防范 虚拟机经常崩= =,所以基本上做一个实验换一个... 遇到了一个简单的小问题老师帮忙解决了:虚拟机没联网... 一.基础问题回答 通常在什么场景下容 ...

  4. 深度学习—caffe框架训练文档

    转存:LMDB E:\机器学习2\caffe资料\caffe_root\caffe-master\Build\x64\Release>convert_imageset.exe E:/机器学习2/ ...

  5. 并行Linq

    有时候我们对大批量数据进行处理,此时并行linq就起作用了. 并行查询 对于以下查询可以耗时会非常大,如下: ; var r = new Random(); , arraySize).Select(x ...

  6. Spring学习(十五)----- Spring AOP通知实例 – Advice

    Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...

  7. MongoDB中设置expire过期自动删除

    关键词: expireAfterSeconds.TTL TTL Time to Live 类似Redis中的expire机制,MongoDB也可以设置过期自动删除的表. MongoDB的过期设置依赖索 ...

  8. SpringBoot之MongoTemplate的查询可以怎么耍

    学习一个新的数据库,一般怎么下手呢?基本的CURD没跑了,当可以熟练的增.删.改.查一个数据库时,可以说对这个数据库算是入门了,如果需要更进一步的话,就需要了解下数据库的特性,比如索引.事物.锁.分布 ...

  9. Charles 抓包使用教程

    将 Charles 设置成系统代理 Charles 主界面介绍 过滤网络请求 截取 iPhone 上的网络封包 截取 Https 通讯信息 模拟慢速网络 修改网络请求内容 给服务器做压力测试 修改服务 ...

  10. junit测试类防止事务回滚-工作心得

    本随笔文章,由个人博客(鸟不拉屎)转移至博客园 发布时间: 2018 年 12 月 06 日 原地址:https://niaobulashi.com/archives/junit-test-rollb ...