题目地址: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. cf591B Rebranding

    B. Rebranding time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  2. 高性能web系统的架构和系统优化

    07年毕业一直都在软件公司,14年来到一个互联网公司,给我的感受,区别主要在于: 软件公司需求相对稳定,能够按照计划按部就班的去实施,互联网公司需求相对来说不稳定,上线比较着急,大部分都是小迭代更新, ...

  3. Hadoop2以来的历次升级(不含2.2.0及以下)

    2015年7月06:release 2.7.1(稳定)请参阅 Hadoop 2.7.1发布说明 对131个bug修复和列表 从先前版本2.7.0补丁. 请看看 2.7.0章节列表的增强功能启用 第一个 ...

  4. Swift基础语法学习总结二

    1.函数 1.1 func funcNmae()->(){} 这样就定义了一个函数,它的参数为空,返回值为空,如果有参数和返回值直接写在两个括号里就可以了 1.2 参数需要指明类型,而如果没有返 ...

  5. sts 去掉启动的rss功能

    STS(spring tools suite) 每次开启都显示,比较烦人,可以在设置中禁用掉,具体方法:window->Perferences,然后查找dashboard,将启动时展示dashb ...

  6. 我理解的javascript单线程机制

    废话不多说,我们先来看几个例子: 1. setTimeout( console.log(2); result:  2 1   2.   console.log(100 setTimeout( cons ...

  7. maven项目中找不到Maven Dependencies解决办法

    用eclipse创建maven项目后,在Deployment Assembly中通过Add...->Java Build Path Entries导入Maven Dependencies时,发现 ...

  8. WPF自定义数字输入框控件

    要求:只能输入数字和小数点,可以设置最大值,最小值,小数点前长度,小数点后长度(支持绑定设置): 代码如下: using System; using System.Collections.Generi ...

  9. (转)Maven实战(三)Eclipse构建Maven项目

    1. 安装m2eclipse插件    要用Eclipse构建Maven项目,我们需要先安装meeclipse插件    点击eclipse菜单栏Help->Eclipse Marketplac ...

  10. ubuntu eclipse CDT 问题

    问题一:ubuntu eclipse c++ launch failed binary not found 解决:建完项目后 查看在项目中是不是有debug目录,说明没有编译.仅仅是须要做例如以下操作 ...