题目链接:

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

题目大意:

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

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

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

代码:

// Problem#: 1016
// Submission#: 3585804
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <string.h> //
int rule[][], m[];
long long step, length;
char num[], target[];
int targetNum[], pro[][];
int answerNum, ans1[], ans2[]; int MAX() {
if (length < ) return m[length];
else return ;
} void init() {
for (int i = ; i < ; i++) {
int temp = i;
for (int j = ; j < ; j++) {
rule[i][j] = temp & ;
temp >>= ;
}
}
m[] = ;
for (int i = ; i < ; i++) m[i] = m[i - ] << ;
} bool check() {
length = strlen(target);
if (length < ) return false;
for (int i = ; i < length; i++) {
if (target[i] == 'W') targetNum[i] = ;
else if (target[i] == 'B') targetNum[i] = ;
else return false;
}
if ((targetNum[] == ) || (targetNum[length - ] == ) || (length & == )) return false;
return true;
} int main() {
init(); int caseNum = ; while () { scanf("%s%s", num, target); if (strcmp(num, "END") == ) break; printf("LINE %d ", caseNum++); if (!check()) {
printf("NONE\n");
continue;
} step = ;
for (int i = ; num[i] != '\0'; i++) step = step * + num[i] - '';
if (step > MAX()) step = MAX(); answerNum = ; for (int i = ; i < ; i++) {
memset(pro, , sizeof(pro));
int last = , now = ;
int s = ;
int jlength = length - , ruleNum;
pro[now][length / ] = ;
while () { if (s > step) break; bool isSame = true;
for (int k = ; k < length; k++) {
if (pro[now][k] != targetNum[k]) {
isSame = false;
break;
}
}
if (isSame) {
ans1[answerNum] = i;
ans2[answerNum] = s;
answerNum++;
break;
}
now ^= ;
last ^= ; s++;
for (int j = ; j < jlength; j++) {
ruleNum = pro[last][j];
ruleNum <<= ;
ruleNum += pro[last][j + ];
ruleNum <<= ;
ruleNum += pro[last][j + ];
pro[now][j + ] = rule[i][ruleNum];
}
pro[now][] = pro[now][length - ] = ;
}
}
if (answerNum) {
for (int i = ; i < answerNum; i++) printf("(%d,%d)", ans1[i], ans2[i]);
printf("\n");
} else printf("NONE\n");
}
return ;
}

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

我的代码:

#include <iostream>
#include <map>
#include <vector>
#include <string.h>
using namespace std; struct Ans {
int mo, t;
Ans(){}
Ans(int a, int b) {
mo = a;
t = b;
}
};
//0 for white, 1 for black
int model[];
map<string, int> transfer;
map<string, int> record;
vector<Ans> ans;
int Max;
string line;
string ary, dest, ary2;
int l; void addModel() {
int i = , j;
while (i >= && model[i]) {
i--;
}
for (j = i; j <= ; j++) {
model[j] = -model[j];
}
} int toInt(string tmp) {
int i, tot = ;
for (i = ; i < tmp.length(); i++) {
tot = tot* + tmp[i] - '';
}
return tot;
} void getData(int& n, string& r) {
string tmp1, tmp2;
tmp1 = tmp2 = "";
int i;
for (i = ; r[i] != ' '; i++) {
tmp1 += r[i];
}
for (i++; i<r.length(); i++) {
tmp2 += r[i];
}
n = toInt(tmp1);
dest = tmp2;
l = dest.length();
} bool cut(int f) {
string tmp;
int i;
for (i = ; i < ; i++) {
tmp += ary2[f+i];
}
return model[transfer[tmp]];
} void Do() {
int i;
ary2 = ary;
for (i = ; i < l-; i++) {
if (cut(i)) {
ary[i+] = 'B';
} else {
ary[i+] = 'W';
}
}
} bool equal(const string& a, const string& b) {
int i;
for (i = ; i < l; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
} int main() {
transfer["BBB"] = ;
transfer["BBW"] = ;
transfer["BWB"] = ;
transfer["BWW"] = ;
transfer["WBB"] = ;
transfer["WBW"] = ;
transfer["WWB"] = ;
transfer["WWW"] = ;
int cnt = ;
while (getline(cin, line)) {
cnt++;
if (line == "END OF INPUT")
break;
ans.clear();
getData(Max, line);
int i, j, k;
for (i = ; i < ; i++) {
ary = "";
for (k = ; k < l; k++) {
ary += 'W';
}
ary[l/] = 'B';
record.clear();
for (j = ; j < Max; j++) {
if (record[ary]) {
break;
}
if (equal(ary, dest)) {
ans.push_back(Ans(i, j+));
break;
} else {
record[ary] = ;
}
Do();
}
addModel();
}
cout << "LINE " << cnt << " ";
for (i = ; i < ans.size(); i++) {
cout << "(" << ans[i].mo << "," << ans[i].t << ")";
}
if (!ans.size()) {
cout << "NONE";
}
cout << endl;
}
}

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. *HDU1251 字典树

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  2. Struts2中的ModelDriven机制及其运用

    所谓ModelDriven,意思是直接把实体类当成页面数据的收集对象.比如,有实体类User如下: package cn.com.leadfar.struts2.actions; public cla ...

  3. Python脚本配合Linux计划任务工作

    经常遇到直接运行Python脚本没有问题,但是一放入/etc/crontab之后就歇菜的情况,总结了一下,大致需要注意以下几点: 1. 脚本首行加入#!/usr/bin/env python 2. 脚 ...

  4. PHP 常用的header头部定义汇总

    <?phpheader('HTTP/1.1 200 OK'); // ok 正常访问header('HTTP/1.1 404 Not Found'); //通知浏览器 页面不存在header(' ...

  5. 一起来做webgame,《Javascript贪食蛇》

    2019-09-22更新: 使用canvas实现:https://github.com/onlyfu/SnakeSir-Javascript 以下为HTML4实现: 今天来个略有意思的,<贪食蛇 ...

  6. sql server 存储过程 以及java如何使用存储过程

    Sql 语句 有一个test_table1表  他有两个字段  ID 和name proc是procedure的缩写 也就是存储过程,StuProc2为创建的存储过程名称 执行以下创建存储后会在Sql ...

  7. Windows下使用WSRM限制MongoDB内存

    有个项目用到了MongoDB,我们是在WINDOWS 2008 64位环境下部署的,为啥不部署到linux下面呢,我们没那么多服务器,只能将就一下了. 大家都知道Mongodb吃内存太厉害了,如果不重 ...

  8. 自动化服务安装部署工具-Ansible

    自动化运维工具Ansible详细部署 ================================================================================= ...

  9. Ms sql 2005 中的bit 数据类型

    bit 整型数据 1.0 或 NULL(在表中的表现形式). 注释: 不能对 bit 类型的列使用索引. Microsoft® SQL Server™ 优化用于 bit 列的存储.如果一个表中有不多于 ...

  10. vim - save current file with a new name but keep editing current file

    http://superuser.com/questions/414110/vim-save-a-file-as-a-different-filename-but-keep-w-as-the-curr ...