题目地址:1282. Computer Game

思路:

KMP算法,网上有很多资料,参考了一些网上的解题,收获很大,很感谢那些大神们!!!

通过这道题简单说说我对KMP算法的理解吧(大神们勿喷,虽然没人看我的orz~~~~囧)。

首先输入的是要匹配的字符串,如果这个字符串的首字母在整个字符串不重复出现的话,直接一直匹配下去即可。

诶,那重复出现了怎么办,那就从最后一个重复出现的重新开始匹配,那么这就找到优化算法的好方法了,KMP算法的精髓大致是这样的。

这道题具体代码如下:

 #include <iostream>
#include <cstdio>
using namespace std; int main() {
int len1, len2;
while (cin >> len1) {
int code[] = {};
int next[] = {};
for (int i = ; i <= len1; i++) {
scanf("%d", &code[i]);
}
int index = ;
for (int i = ; i <= len1; i++) {
index = code[index+] == code[i] ? index+ : ;
next[i] = index;
}
cin >> len2;
index = ;
int test, result;
for (int i = ; i <= len2; i++) {
scanf("%d", &test);
if (index != len1) {
index = code[index+] == test ? index+ : next[index];
if (index == len1) {
result = i-len1;
}
}
}
index == len1 ? cout << result << endl : cout << "no solution\n";
} return ;
}

Sicily 1282. Computer Game的更多相关文章

  1. [SOJ] 1282. Computer games (KMP)

    坑爹题 1282. Computer Game Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Brian is an ...

  2. 大数求模 sicily 1020

        Search

  3. 共享文件夹:The user has not been granted the requested logon type at this computer

    场景重现 今天做一个项目测试,要用到虚拟机,于是在虚拟机(XP 32)上新建了一个共享的文件夹.然后我在Win7 机器上访问它得到如下的error 消息:

  4. sicily 中缀表达式转后缀表达式

    题目描述 将中缀表达式(infix expression)转换为后缀表达式(postfix expression).假设中缀表达式中的操作数均以单个英文字母表示,且其中只包含左括号'(',右括号‘)’ ...

  5. AC日记——约瑟夫问题 codevs 1282

    1282 约瑟夫问题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解  查看运行结果     题目描述 Description 有编号从1到N的N个小 ...

  6. Computer assisted surgery

    Computer assisted surgery (CAS) represents a surgical concept and set of methods, that use computer ...

  7. Computer vision labs

    积累记录一些视觉实验室,方便查找 1.  多伦多大学计算机科学系 2.  普林斯顿大学计算机视觉和机器人实验室 3.  牛津大学Torr Vision Group 4.  伯克利视觉和学习中心 Pro ...

  8. Computer Vision: OpenCV, Feature Tracking, and Beyond--From <<Make Things See>> by Greg

    In the 1960s, the legendary Stanford artificial intelligence pioneer, John McCarthy, famously gave a ...

  9. 数论 - Vanya and Computer Game

    Vanya and his friend Vova play a computer game where they need to destroy n monsters to pass a level ...

随机推荐

  1. Matlab:拟合(2)

    非线性最小二乘拟合: 解法一:用命令lsqcurvefit function f = curvefun(x, tdata) f = x() + x()*exp() * tdata); %其中x() = ...

  2. HTTP学习笔记5-常用报头

    24,在普通报头中,有少数报头域应用域所有的请求和响应消息,但并不用于被传输的实体,这些报头域只用于传输的消息. 25,常用的普通报头Cache-Control: Cache-Control普通报头域 ...

  3. Duplicate headers received from server

    This ones a little old but was high in the google ranking so I thought I would throw in the answer I ...

  4. EMV/PBOC 解析(一) 卡片文件结构

    刚到公司老大便发我一份文档<智能卡ISO7816-4规范(中文版)>,然后就让我研究下IC智能卡数据读取和支付.身为一直做.NET开发的我对硬件啥的一无所知,各种无头绪啊,研究了两天后,稍 ...

  5. [Javascript] Advanced Reduce: Common Mistakes

    Take away: Always check you ruturn the accumulator Always pass in the inital value var data = [" ...

  6. Win7系统安装MySQL5.5.21图解教程

    转自:http://www.jb51.net/article/37310.htm 这篇文章主要介绍了如何在win7中安装mysql,所以加上了MySQL的下载过程,希望对需要的人有所帮助 大家都知道M ...

  7. JAVA - Comparable接口 与 Comparator接口

      Similarities:Both are custom ways to compare two objects.Both return an int describing the relatio ...

  8. poj 1964 Cow Cycling(dp)

    /* 一开始想的二维的 只维护第几只牛还有圈数 后来发现每只牛的能量是跟随每个状态的 所以再加一维 f[i][j][k]表示第i只牛 领跑的j全 已经消耗了k体力 转移的话分两类 1.换一只牛领跑 那 ...

  9. hdu 2186

    #include <iostream> using namespace std; int main() { int a,b,c,k1,k2,k3,m,n; cin>>m; wh ...

  10. UFLDL课程学习(一)

    章节地址:http://ufldl.stanford.edu/tutorial/supervised/LinearRegression/ 章节名称:线性回归 (Linear Regression) 第 ...