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

  1. WWBWBB

样例输出1

  1. 1

样例输入2

  1. WWWWBBW

样例输出2

  1. 0

样例输入3

  1. WBBBBBWWBW

样例输出3

  1. 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

  1. 3
  2. ())
  3. ((()
  4. )()

样例输出1

  1. 10

样例输入2

  1. 5
  2. )))))
  3. )
  4. ((
  5. ))((
  6. (

样例输出2

  1. 2

题目来源

The North American Invitational Programming Contest 2017

代码:

  1. #include <iostream>
  2. #include <cstring>
  3. #include <queue>
  4. using namespace std;
  5. template <class T, class C>
  6. using heap = priority_queue<T, vector<T>, C>;
  7. void abc(string s, int &a, int &b, int &c)
  8. {
  9. a = ,
  10. b = ,
  11. c = s.length();
  12. for (int i = ; i < s.length(); i++)
  13. {
  14. switch (s[i])
  15. {
  16. case '(':
  17. a++;
  18. break;
  19. case ')':
  20. if (a > )
  21. {
  22. a--;
  23. }
  24. else
  25. {
  26. b++;
  27. }
  28. }
  29. }
  30. }
  31. struct triple
  32. {
  33. int a,
  34. b,
  35. c;
  36. };
  37. bool operator>(const triple &A, const triple &B)
  38. {
  39. if (A.b ^ B.b)
  40. {
  41. return A.b > B.b;
  42. }
  43. if (A.a ^ B.a)
  44. {
  45. return A.a < B.a;
  46. }
  47. return A.c < B.c;
  48. }
  49. bool operator<(const triple &A, const triple &B)
  50. {
  51. if (A.a ^ B.a)
  52. {
  53. return A.a > B.a;
  54. }
  55. if (A.b ^ B.b)
  56. {
  57. return A.b < B.b;
  58. }
  59. return A.c < B.c;
  60. }
  61. int main()
  62. {
  63. int n{};
  64. cin >> n;
  65. int A[], B[];
  66. memset(A, 0xf0, sizeof(A));
  67. memset(B, 0xf0, sizeof(B));
  68. A[] = ;
  69. B[] = ;
  70. heap<triple, greater<triple>> I;
  71. heap<triple, less<triple>> D;
  72. for (int i = ; i <= n; i++)
  73. {
  74. string s;
  75. cin >> s;
  76. int a{}, b{}, c{};
  77. abc(s, a, b, c);
  78. if (a >= b)
  79. {
  80. I.push({a, b, c});
  81. }
  82. else
  83. {
  84. D.push({a, b, c});
  85. }
  86. }
  87. while (I.size())
  88. {
  89. const int a = I.top().a,
  90. b = I.top().b,
  91. c = I.top().c;
  92. for (int x = ; x >= max(b, a - b); x--)
  93. {
  94. A[x] = max(A[x], A[x - a + b] + c);
  95. }
  96. I.pop();
  97. }
  98. while (D.size())
  99. {
  100. const int a = D.top().a,
  101. b = D.top().b,
  102. c = D.top().c;
  103. for (int x = ; x >= max(a, b - a); x--)
  104. {
  105. B[x] = max(B[x], B[x - b + a] + c);
  106. }
  107. D.pop();
  108. }
  109. int reponse{};
  110. for (int x = ; x <= ; x++)
  111. {
  112. reponse = max(reponse, A[x] + B[x]);
  113. }
  114. cout << reponse << endl;
  115. return ;
  116. }

参考博客: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. 一、用Delphi10.3 创建一条JSON数据

    一.用Delphi10.3构造一个JSON数据,非常之容易,代码如下: uses System.JSON; procedure TForm1.Button1Click(Sender: TObject) ...

  2. CTF-i春秋网鼎杯第二场misc部分writeup

    CTF-i春秋网鼎杯第二场misc部分writeup 套娃 下载下来是六张图片 直接看并没有什么信息 一个一个查看属性 没有找到有用信息 到winhexv里看一下 都是标准的png图片,而且没有fla ...

  3. C语言入门教程-(5)格式化输入输出

    1.输入和输出 在程序的使用中,我们经常可以看的这么一个场景:用户需要输入数据,经过程序运算,得到结果后输出.在C语言中,输入数据和输出数据都是由库函数完成的,通过语句来输入/输出. 2.格式化输出— ...

  4. Hibernate第一天——入门和基本操作

    第一个接触的框架就是这个Hibernate框架了,Hibernate本意是 冬眠 ,这里有必要引用CSDN上某位网友某个帖子的评论先引出框架的概念: 框架:一个软件半成品,帮你做了一些基础工作,你就可 ...

  5. 数据增强利器--Augmentor

    最近遇到数据样本数目不足的问题,自己写的增强工具生成数目还是不够,终于在网上找到一个数据增强工具包,足够高级,足够傻瓜.想要多少就有多少!再也不怕数据不够了! 简介 Augmentor是一个Pytho ...

  6. zhengruioi 470 区间

    区间 链接 题意:给定n个区间[li,ri].你可以选出任意一些区间,设选出的区间个数是s,[l,r]是这些区间的交,求min(s,r-l+1)的最大值. N≤3×105 分析:首先按照左端点排序,然 ...

  7. Gitlab+Jenkins学习之路(六)之Jenkins部署、升级和备份

    一.什么是持续集成? (1)Continuous integration(CI) 持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员至少集成一次,也就意味着每天可能会发生多次集 ...

  8. 解决web翻转动画闪屏

    首先确保backface-visibility: hidden.这样做可以解决大部分闪屏的情况. 然后需要特别注意的是谷歌的浏览器,不管是桌面端还是移动端,在翻转的过程中在该元素上绘制其他元素也会导致 ...

  9. Permission Policies

    The Permission Policy determines Security System behavior when there are no explicitly specified per ...

  10. Fortran的数组与指针

    个人理解,欢迎指正 指针就是记录数据的内存地址的变量.指针可以指向单个变量,也可以指向数组. 数组是一个概念,是若干个类型相同的元素的有序集合.在Fortran语言中,数组中存放的元素,可以是整数,实 ...