A. Two Substrings

题意:给一个字符串,求是否含有不重叠的子串"AB"和"BA",长度1e5。

题解:看起来很简单,但是一直错,各种考虑不周全,最后只能很蠢的暴力,把所有的AB和BA的位置求出来,能有一对AB和BA不重叠即可。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. char a[];
  5. vector<int> ab;
  6. vector<int> ba;
  7. int main()
  8. {
  9. while (~scanf("%s", a)) {
  10. int len = strlen(a);
  11. ab.clear();
  12. ba.clear();
  13.  
  14. for (int i = ; i < len - ; ++i) {
  15. if (a[i] == 'A' && a[i + ] == 'B') {
  16. ab.push_back(i);
  17. }
  18. if (a[i] == 'B' && a[i + ] == 'A') {
  19. ba.push_back(i);
  20. }
  21. }
  22.  
  23. if (ab.size() == || ba.size() == ) {
  24. puts("NO");
  25. } else if (ab.size() > && ba.size() > ) {
  26. puts("YES");
  27. } else {
  28. int flag = false;
  29. for (int i = ; i < ab.size(); ++i) {
  30. for (int j = ; j < ba.size(); ++j) {
  31. if (!(ab[i] + == ba[j]) && !(ab[i] - == ba[j])) flag = true;
  32. }
  33. }
  34. if (flag) puts("YES");
  35. else puts("NO");
  36. }
  37. }
  38. return ;
  39. }

B. Preparing Olympiad

题意:输入n, l, r, x,代表有n题,每题有一个难度,选其中部分题(大于2题),要求难度和在[l, r]之间,最大难度最小难度的差不超过x。1 ≤ n ≤ 15

题解:暴力,dfs求全部情况。复杂度2^15

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int a[];
  6. int n, l, r, x;
  7. int vis[];
  8. int ans;
  9.  
  10. bool ok()
  11. {
  12. int cnt = ;
  13. for (int i = ; i < n; ++i) if (vis[i]) cnt++;
  14. if (cnt < ) return false;
  15. int low = ;
  16. int high = ;
  17. int sum = ;
  18. for (int i = ; i < n; ++i) {
  19. if (!vis[i]) continue;
  20. low = min(low, a[i]);
  21. high = max(high, a[i]);
  22. sum += a[i];
  23. }
  24. if (high - low >= x && sum <= r && sum >= l) return true;
  25. return false;
  26. }
  27.  
  28. void dfs(int pos)
  29. {
  30. if (pos == n) {
  31. if (ok()) ans++;
  32. return ;
  33. }
  34. vis[pos] = ;
  35. dfs(pos + );
  36. vis[pos] = ;
  37. dfs(pos + );
  38. }
  39.  
  40. int main()
  41. {
  42. while (cin >> n >> l >> r >> x) {
  43. for (int i = ; i < n; ++i) cin >> a[i];
  44. ans = ;
  45. dfs();
  46. cout << ans << endl;
  47. }
  48. return ;
  49. }

C. Divisibility by Eight

题意:给一个不超过100位的没有前导零的非负整数,问可不可能移出一部分数字,事剩下至少一位非负整数能被8整除。

题解:能被8整除的数有一个特性:一个整数的末3位若能被8整除,则该数一定能被8整除。求出所有的三位以内能被8整除的数,分别试一下就ok了。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int ans[];
  5. char a[];
  6. //一个整数的末3位若能被8整除,则该数一定能被8整除。
  7. bool ok(int x)
  8. {
  9. int num[];
  10. int cnt = ;
  11. while (x) {
  12. num[cnt++] = x % ;
  13. x /= ;
  14. }
  15. reverse(num, num + cnt);
  16. int len = strlen(a);
  17. int pos = ;
  18. int i;
  19. for (i = ; i < cnt; ++i) {
  20. while (a[pos] - '' != num[i] && pos < len) {
  21. pos++;
  22. }
  23. if (pos >= len) break;pos++;
  24. }
  25. if (i == cnt) return true;
  26. return false;
  27. }
  28.  
  29. int main()
  30. {
  31. int idx = ;
  32. ans[] = ;
  33. for (int i = ; i < ; ++i) {
  34. if (i * > ) break;
  35. ans[idx++] = i * ;
  36. }
  37.  
  38. scanf("%s", a);
  39. for (int i = ; i < idx; ++i) {
  40. if (ok(ans[i])) {
  41. printf("YES\n%d\n", ans[i]);
  42. return ;
  43. }
  44. }
  45. int len = strlen(a);
  46. for (int i = ; i < len; ++i) {
  47. if (a[i] == '') {
  48. printf("YES\n0\n");
  49. return ;
  50. }
  51. }
  52. printf("NO\n");
  53.  
  54. return ;
  55. }

D. Regular Bridge (构造)

题意:给一个k(1 ≤ k ≤ 100) ,问是否存在一个无向图至少有一个桥且所有点的度都是k。如果存在给出任意一种解。

题解:对于k+1个点的完全图,所有点的度都是k。考虑两堆k+1的点,然后再用两个点组成桥,每个点和其中一个堆连k-1就可以了。每次桥上的点连堆中两个点,然后删除这两个点的连线,度数不变。同时也可以发现只有k-1为偶数是,即k为奇数时才有解。

  1. #include <cstdio>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int k;
  8. while (~scanf("%d", &k)) {
  9. if ((k & ) == ) {
  10. puts("NO");
  11. } else if (k == ) {
  12. puts("YES\n2 1\n1 2");
  13. } else {
  14. puts("YES");
  15. printf("%d %d\n", * k + , k * k + * k);
  16. for (int i = ; i < k; ++i) {
  17. printf("%d %d\n", * k + , i);
  18. printf("%d %d\n", * k + , i + k + );
  19. }
  20. printf("%d %d\n", * k + , * k + );
  21. for (int i = ; i <= k + ; ++i) {
  22. for (int j = i + ; j <= k + ; ++j) {
  23. if (i < k - && (i & ) && i + == j) continue;
  24. printf("%d %d\n", i, j);
  25. printf("%d %d\n", k + + i, k + + j);
  26. }
  27. }
  28. }
  29. }
  30. return ;
  31. }

E. Brackets in Implications(构造)

题意:给定01间运算0->0=1  0->1=1  1->0=0  1->1=1 默认从左到右运算 问能不能通过加括号使得表达式值变为0

题解:很容易发现最后一位必须是0,倒数第二位如果是1,那么不用加括号直接为0。如果倒数第二位为0,那么需要在前面找到一个0,两个0即中间的1组成1,然后同上。两个0和中间的1组成1的方法可以为(0(11...110))。

  1. #include <iostream>
  2. #include <stdio.h>
  3.  
  4. using namespace std;
  5.  
  6. int a[];
  7. int n;
  8. int main()
  9. {
  10. cin >> n;
  11. for (int i = ; i < n; ++i)
  12. scanf("%d", a + i);
  13.  
  14. if (n == ) {
  15. if (a[] == ) puts("YES\n0");
  16. else puts("NO");
  17. } else if (n == ) {
  18. if (a[] == && a[] == ) puts("YES\n1->0");
  19. else puts("NO");
  20. } else if (a[n - ] == ) {
  21. puts("NO");
  22. } else {
  23. if (a[n - ] == ) {
  24. puts("YES");
  25. for (int i = ; i < n - ; ++i) {
  26. printf("%d->", a[i]);
  27. }
  28. puts("");
  29. } else {
  30. int pos = -;
  31. for (int i = n - ; i >= ; --i) {
  32. if (a[i] == ) {
  33. pos = i;
  34. break;
  35. }
  36. }
  37. if (pos == -) puts("NO");
  38. else {
  39. puts("YES");
  40. for (int i = ; i < pos; ++i) {
  41. printf("%d->", a[i]);
  42. }
  43. printf("(0->(");
  44. for (int i = pos + ; i < n - ; ++i) {
  45. printf("%d->", a[i]);
  46. }
  47. puts("0))->0");
  48. }
  49. }
  50. }
  51. return ;
  52. }

Codeforces Round #306 (Div. 2) ABCDE(构造)的更多相关文章

  1. Codeforces Round #261 (Div. 2)[ABCDE]

    Codeforces Round #261 (Div. 2)[ABCDE] ACM 题目地址:Codeforces Round #261 (Div. 2) A - Pashmak and Garden ...

  2. 数学/找规律/暴力 Codeforces Round #306 (Div. 2) C. Divisibility by Eight

    题目传送门 /* 数学/暴力:只要一个数的最后三位能被8整除,那么它就是答案:用到sprintf把数字转移成字符读入 */ #include <cstdio> #include <a ...

  3. DFS Codeforces Round #306 (Div. 2) B. Preparing Olympiad

    题目传送门 /* DFS: 排序后一个一个出发往后找,找到>r为止,比赛写了return : */ #include <cstdio> #include <iostream&g ...

  4. 水题 Codeforces Round #306 (Div. 2) A. Two Substrings

    题目传送门 /* 水题:遍历一边先找AB,再BA,再遍历一边先找BA,再AB,两种情况满足一种就YES */ #include <cstdio> #include <iostream ...

  5. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  6. Codeforces Round #306 (Div. 2) E. Brackets in Implications 构造

    E. Brackets in Implications Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/conte ...

  7. Codeforces Round #306 (Div. 2) D. Regular Bridge 构造

    D. Regular Bridge Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/550/pro ...

  8. Codeforces Round #306 (Div. 2)A B C D 暴力 位/暴力 暴力 构造

    A. Two Substrings time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  9. Codeforces Round #306 (Div. 2) C. Divisibility by Eight 暴力

    C. Divisibility by Eight Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...

随机推荐

  1. uCGUI字符串显示过程分析和uCGUI字库的组建

    为什么要分析字符串的显示过程? 学习uCGUI主要是学习如何使用的,为何要深究到源码的层次呢? 就分析字符串显示过程的原因来说,是因为移植汉字字库的需要.uCGUI并么有合适的汉字字库,而且完整的汉字 ...

  2. MVC-登录并设置角色

    1.新建一个类,设置角色: using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  3. poj 1348 Computing (四个数的加减乘除四则运算)

    http://poj.org/problem?id=1348 Computing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions ...

  4. 关于 IIS 上的 Speech 设置

    在之前的开发过程中,发现在 微软各个版本的Speech中(从sdk5.1 到 最新的 Speech PlatFarme V11),在本地可以生成音频文件,但是在IIS上却生成无法生成完整的文件. 调试 ...

  5. JavaScript跨站脚本攻击

    跨站脚本攻击(Cross-Site Scrpting)简称为XSS,指的是向其他域中的页面的DOM注入一段脚本,该域对其他用户可见.恶意用户可能会试图利用这一弱点记录用户的击键或操作行为,以窃取用户的 ...

  6. 练习PYTHON之EPOLL

    哟,哟,哟,,SELECT,EPOLL之类的,终于出现了. 不能太急了,要缓一缓,缓一缓,再缓一缓~~~~~~~~~ http://scotdoyle.com/python-epoll-howto.h ...

  7. MSSQL版本

    (1)661是sql2008 R2的版本号     Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (Intel X86)   Apr  2 201 ...

  8. Android EditText多行显示及所有属性

    android:id="@+id/editSms" android:layout_width="fill_parent" android:layout_heig ...

  9. hadoop2.2编程:各种API

    hadoop2.2 API http://hadoop.apache.org/docs/r0.23.9/api/index.html junit API http://junit.org/javado ...

  10. 深入浅MFC

    视图类CView 在MFC"文档/视图"架构中,CView类是所有视图类的基类,它提供了用户自定义视图类的公共接口.在"文档/视图"架构中,文档负责管理和维护数 ...