题目地址: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. Visual Studio Code 与 Github 集成

    使用Visual Studio Code进行Nodejs开发充满了便利,为了更好的进行开发工作,有必要使用Github进行代码管理. Visual Studio Code已经集成了GIT组件: htt ...

  2. 【索引】gtest学习笔记

    下载gtest 链接:http://www.cnblogs.com/duxiuxing/p/4270804.html gtest官方文档浅析 链接:http://www.cnblogs.com/dux ...

  3. C++ Template Specialization (模板特化)

    个人理解这个东西说白了就是当模板类(或函数)的类型参数为某特定值时用对应的特化定义代之. 看个例子吧 #include <iostream> using namespace std; te ...

  4. YKCW6-BPFPF-BT8C9-7DCTH-QXGWCYQ7PR-QTHDM-HCBCV-9GKGG-TB2TM

    YKCW6-BPFPF-BT8C9-7DCTH-QXGWCYQ7PR-QTHDM-HCBCV-9GKGG-TB2TM

  5. Android Studio中解决Gradle DSL method not found: &#39;android()&#39;

    近期导入as的项目出了这种问题 这个问题困扰了我非常长时间,好吧,搜了半天全都是runProguard的.最后在stackoverflow上搜到解决的方法了: http://stackoverflow ...

  6. bullet HashMap 内存紧密的哈希表

    last modified time:2014-11-9 14:07:00 bullet 是一款开源物理引擎,它提供了碰撞检測.重力模拟等功能,非常多3D游戏.3D设计软件(如3D Mark)使用它作 ...

  7. 论文摘抄 - FlumeJava

    本摘抄不保证论文完整性和理解准确性  原始的MapReduce.分Map,Shuffle,Reduce. Map里包含shards. Shuffle理解为groupByKey的事情.Reduce里包含 ...

  8. 27个Jupyter Notebook使用技巧及快捷键(翻译版)

    Jupyter Notebook Jupyter Notebook 以前被称为IPython notebook.Jupyter Notebook是一款能集各种分析包括代码.图片.注释.公式及自己画的图 ...

  9. CentOS 7 修改hostname

    centOS 7 里面修改hostname的方式有所改变,修改/etc/hosts和/etc/sysconfig/network两个文件已经不能生效.使用的新命令是 : hostnamectl set ...

  10. Linux UDP严重丢包问题的解决

    测试系统在Linux上的性能发现丢包率极为严重,发210000条数据,丢包达110000之巨,丢包率超过50%.同等情形下Windows上测试,仅丢几条数据.形势严峻,必须解决.考虑可能是因为协议栈B ...