题目链接:

http://soj.sysu.edu.cn/1016

题目大意:

给定一个字符串和256条规则,将某条规则应用于字符串,字符串将发生变化,给定一个数max,求出在max步内可以将字符串变为指定字符串的规则号以及步数。

题目分析:
既然只有256条规则,而且步数又有限制,那我们就暴力模拟,把所有情况都求出来就行了,但是如果用原始字符串来做是非常慢的,虽然题目的时间限制很宽松(10s),但也是会超时的。所以对于每个规则我用一个record的map来保存已经遇到过的字符串,这样就可以过了(虽然还是很慢)。

在网上看到的做法是用0,1分别代表白黑的,而且用到了很多位运算,虽然也是暴力,但是快很多,一样的测试,人家0.15s就过了。

代码:

  1. // Problem#: 1016
  2. // Submission#: 3585804
  3. // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
  4. // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
  5. // All Copyright reserved by Informatic Lab of Sun Yat-sen University
  6. #include <stdio.h>
  7. #include <string.h>
  8.  
  9. //
  10. int rule[][], m[];
  11. long long step, length;
  12. char num[], target[];
  13. int targetNum[], pro[][];
  14. int answerNum, ans1[], ans2[];
  15.  
  16. int MAX() {
  17. if (length < ) return m[length];
  18. else return ;
  19. }
  20.  
  21. void init() {
  22. for (int i = ; i < ; i++) {
  23. int temp = i;
  24. for (int j = ; j < ; j++) {
  25. rule[i][j] = temp & ;
  26. temp >>= ;
  27. }
  28. }
  29. m[] = ;
  30. for (int i = ; i < ; i++) m[i] = m[i - ] << ;
  31. }
  32.  
  33. bool check() {
  34. length = strlen(target);
  35. if (length < ) return false;
  36. for (int i = ; i < length; i++) {
  37. if (target[i] == 'W') targetNum[i] = ;
  38. else if (target[i] == 'B') targetNum[i] = ;
  39. else return false;
  40. }
  41. if ((targetNum[] == ) || (targetNum[length - ] == ) || (length & == )) return false;
  42. return true;
  43. }
  44.  
  45. int main() {
  46. init();
  47.  
  48. int caseNum = ;
  49.  
  50. while () {
  51.  
  52. scanf("%s%s", num, target);
  53.  
  54. if (strcmp(num, "END") == ) break;
  55.  
  56. printf("LINE %d ", caseNum++);
  57.  
  58. if (!check()) {
  59. printf("NONE\n");
  60. continue;
  61. }
  62.  
  63. step = ;
  64. for (int i = ; num[i] != '\0'; i++) step = step * + num[i] - '';
  65. if (step > MAX()) step = MAX();
  66.  
  67. answerNum = ;
  68.  
  69. for (int i = ; i < ; i++) {
  70. memset(pro, , sizeof(pro));
  71. int last = , now = ;
  72. int s = ;
  73. int jlength = length - , ruleNum;
  74. pro[now][length / ] = ;
  75. while () {
  76.  
  77. if (s > step) break;
  78.  
  79. bool isSame = true;
  80. for (int k = ; k < length; k++) {
  81. if (pro[now][k] != targetNum[k]) {
  82. isSame = false;
  83. break;
  84. }
  85. }
  86. if (isSame) {
  87. ans1[answerNum] = i;
  88. ans2[answerNum] = s;
  89. answerNum++;
  90. break;
  91. }
  92. now ^= ;
  93. last ^= ;
  94.  
  95. s++;
  96. for (int j = ; j < jlength; j++) {
  97. ruleNum = pro[last][j];
  98. ruleNum <<= ;
  99. ruleNum += pro[last][j + ];
  100. ruleNum <<= ;
  101. ruleNum += pro[last][j + ];
  102. pro[now][j + ] = rule[i][ruleNum];
  103. }
  104. pro[now][] = pro[now][length - ] = ;
  105. }
  106. }
  107. if (answerNum) {
  108. for (int i = ; i < answerNum; i++) printf("(%d,%d)", ans1[i], ans2[i]);
  109. printf("\n");
  110. } else printf("NONE\n");
  111. }
  112. return ;
  113. }

这提示我以后能用int尽量不用字符串。多考虑效率。这道题要是时间限制到1s我就GG了。

我的代码:

  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4. #include <string.h>
  5. using namespace std;
  6.  
  7. struct Ans {
  8. int mo, t;
  9. Ans(){}
  10. Ans(int a, int b) {
  11. mo = a;
  12. t = b;
  13. }
  14. };
  15. //0 for white, 1 for black
  16. int model[];
  17. map<string, int> transfer;
  18. map<string, int> record;
  19. vector<Ans> ans;
  20. int Max;
  21. string line;
  22. string ary, dest, ary2;
  23. int l;
  24.  
  25. void addModel() {
  26. int i = , j;
  27. while (i >= && model[i]) {
  28. i--;
  29. }
  30. for (j = i; j <= ; j++) {
  31. model[j] = -model[j];
  32. }
  33. }
  34.  
  35. int toInt(string tmp) {
  36. int i, tot = ;
  37. for (i = ; i < tmp.length(); i++) {
  38. tot = tot* + tmp[i] - '';
  39. }
  40. return tot;
  41. }
  42.  
  43. void getData(int& n, string& r) {
  44. string tmp1, tmp2;
  45. tmp1 = tmp2 = "";
  46. int i;
  47. for (i = ; r[i] != ' '; i++) {
  48. tmp1 += r[i];
  49. }
  50. for (i++; i<r.length(); i++) {
  51. tmp2 += r[i];
  52. }
  53. n = toInt(tmp1);
  54. dest = tmp2;
  55. l = dest.length();
  56. }
  57.  
  58. bool cut(int f) {
  59. string tmp;
  60. int i;
  61. for (i = ; i < ; i++) {
  62. tmp += ary2[f+i];
  63. }
  64. return model[transfer[tmp]];
  65. }
  66.  
  67. void Do() {
  68. int i;
  69. ary2 = ary;
  70. for (i = ; i < l-; i++) {
  71. if (cut(i)) {
  72. ary[i+] = 'B';
  73. } else {
  74. ary[i+] = 'W';
  75. }
  76. }
  77. }
  78.  
  79. bool equal(const string& a, const string& b) {
  80. int i;
  81. for (i = ; i < l; i++) {
  82. if (a[i] != b[i]) {
  83. return false;
  84. }
  85. }
  86. return true;
  87. }
  88.  
  89. int main() {
  90. transfer["BBB"] = ;
  91. transfer["BBW"] = ;
  92. transfer["BWB"] = ;
  93. transfer["BWW"] = ;
  94. transfer["WBB"] = ;
  95. transfer["WBW"] = ;
  96. transfer["WWB"] = ;
  97. transfer["WWW"] = ;
  98. int cnt = ;
  99. while (getline(cin, line)) {
  100. cnt++;
  101. if (line == "END OF INPUT")
  102. break;
  103. ans.clear();
  104. getData(Max, line);
  105. int i, j, k;
  106. for (i = ; i < ; i++) {
  107. ary = "";
  108. for (k = ; k < l; k++) {
  109. ary += 'W';
  110. }
  111. ary[l/] = 'B';
  112. record.clear();
  113. for (j = ; j < Max; j++) {
  114. if (record[ary]) {
  115. break;
  116. }
  117. if (equal(ary, dest)) {
  118. ans.push_back(Ans(i, j+));
  119. break;
  120. } else {
  121. record[ary] = ;
  122. }
  123. Do();
  124. }
  125. addModel();
  126. }
  127. cout << "LINE " << cnt << " ";
  128. for (i = ; i < ans.size(); i++) {
  129. cout << "(" << ans[i].mo << "," << ans[i].t << ")";
  130. }
  131. if (!ans.size()) {
  132. cout << "NONE";
  133. }
  134. cout << endl;
  135. }
  136. }

1016. Boundaries on A New Kind of Science 解题报告的更多相关文章

  1. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  2. 转载 ACM训练计划

    leetcode代码 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode. ...

  3. (转)POJ题目分类

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  4. poj分类

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

  5. POJ1325 Machine Schedule

    Description As we all know, machine scheduling is a very classical problem in computer science and h ...

  6. POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)

    本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...

  7. POJ题目分类(转)

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  8. ACM学习

    转:ACM大量习题题库   ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库.   US ...

  9. POJ3974 Palindrome

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

随机推荐

  1. 【Alpha】Daily Scrum Meeting第三次

    本次随笔调换了展示顺序,把重要的内容放前面. 一.本次Daily Scrum Meeting主要内容 说明要完成alpha版本还需要哪些功能 对这些功能进行分析和实现方式的讨论 强调编码规范和变量命名 ...

  2. ImportError: The _imagingft C module is not installed

    添加验证码模块的时候,发布到服务器上居然报了这个错误 ImportError: The _imagingft C module is not installed 然而pillow是已经装在服务器上的, ...

  3. 使用Hibernate SQLQuery(转)

    原文地址:http://itindex.net/detail/51776-hibernate-sqlquery-sql,重新排了一下版 Hibernate对原生SQL查询的支持和控制是通过SQLQue ...

  4. java常用集合框架底层实现简介与注意点

    Collection: ArrayList:1:底层实现是数组,默认长度是10.2:add(),判断是否数组越界,是数组扩容为原来的两倍.3:remove(),copy数组,size-1,释放空虚的空 ...

  5. BAT常用脚本汇总

    1.取得时间戳 @echo off set date0=%date:~0,10% set hour0=%time:~0,2% set time0=%time:~0,2%%time:~3,2%%time ...

  6. h5的离线缓存机制

    什么是Manifest: 其实Manifest是一个简单的 文本文件,它的扩展名是任意的,定义需要缓存的文件.资源,当第一次打开时,浏览器会自动缓存相应的资源. Manifest 的特点: 离线浏览: ...

  7. php面试题

    1.用PHP打印出前一天的时间格式是2006-5-10 22:21:21(2分) <?php date_default_timezone_set('Asia/shanghai'); echo d ...

  8. phyton 相关学习

    http://www.nowamagic.net/academy/category/13/ http://www.runoob.com/python/python-reg-expressions.ht ...

  9. springmvc 动态代理 JDK实现与模拟JDK纯手写实现。

    首先明白 动态代理和静态代理的区别: 静态代理:①持有被代理类的引用  ② 代理类一开始就被加载到内存中了(非常重要) 动态代理:JDK中的动态代理中的代理类是动态生成的.并且生成的动态代理类为$Pr ...

  10. Python开发【前端】:汇总

    页面模板 1.EasyUI(推荐指数★) JQuery EasyUI中文网 下载 使用方法:把文件下载到本地.直接从官网上把源码拷贝过来,更改下js的路径即可 优点:功能非常多.非常齐全 偏做后台管理 ...