坑爹题

1282. Computer Game

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

Brian is an enthusiast of computer games, especially those that simulate virtual reality. Now he is in front of the Star Gate. In order to open the gate he must break the protection as quickly as he can. Breaking the protection means to match a given code (a sequence of numbers) against the gate protection (a very long sequence of numbers). The starting position of the first occurrence of the code within the sequence opens the gate. Can you help him?

The code is a sequence of at most 60000 integer numbers between 0 and 255. The gate protection contains integer numbers between 0 and 255. Your program must find the first match if there is one, or report the absence of a match.

Input

The text input file contains several data sets. Each data set has the following format:

l     the length of the code

l     the sequence of numbers representing the code

l     the number of integers in the gate protection

l     the sequence of numbers representing the gate protection 

code_dimension
integerinteger2 … integercode_dimension 
protection_dimension
integerinteger2 … integerprotection_dimension

White spaces may occur freely in the input.

Output

The results must be printed on the standard output. For each given data set, print the result on a separate line. The result is a number that represents the position (starting from zero) of the first occurrence of the code in the gate protection, or the message no solution if there is no match.

Sample Input

3
0 1 2
7
2 3 4 0 1 2 5 2
1 4
6
4 1 4 1 4 4 3
1 2 3
7
3 2 1 3 2 255 7

Sample Output

3
1
no solution
#include<iostream>
#include<stdio.h>
using namespace std; int pat[1000000], text[1000000];
int next0[1000000]; void get_Next(int len2)
{
int k = -1;
int i = 0;
next0[0] = -1; while (i<len2)
{
if (k == -1 || pat[i] == pat[k])
{
k++;
i++;
next0[i] = k;
}
else
k = next0[k];
}
} int KMP(int len1, int len2)
{
int i = 0;
int j = 0; get_Next(len2); while ((i<len1) && (j<len2))
{
if (j == -1 || pat[j] == text[i])
{
i++;
j++;
}
else
j = next0[j];
}
if (j == len2)
return i - j;
return -1;
} int main()
{
int len2;
int len1;
while (scanf("%d", &len2) != EOF)
{
for (int i = 0; i < len2; i++)
cin >> pat[i];
cin >> len1;
for (int i = 0; i < len1; i++)
cin >> text[i]; int q = KMP(len1, len2); if (q >= 0)
cout << q << endl;
else
cout << "no solution" << endl;
} return 0;
}

  

[SOJ] 1282. Computer games (KMP)的更多相关文章

  1. Sicily 1282. Computer Game

    题目地址:1282. Computer Game 思路: KMP算法,网上有很多资料,参考了一些网上的解题,收获很大,很感谢那些大神们!!! 通过这道题简单说说我对KMP算法的理解吧(大神们勿喷,虽然 ...

  2. 论文笔记之:Playing for Data: Ground Truth from Computer Games

    Playing for Data: Ground Truth from Computer Games ECCV 2016 Project Page:http://download.visinf.tu- ...

  3. SOJ 1717 Computer (单机任务调度)

    一.题目描述 Constraints :Time Limit: 2 secs, Memory Limit: 32 MB Description: We often hear that computer ...

  4. Codeforces Round #546 (Div. 2) B. Nastya Is Playing Computer Games

    链接:https://codeforces.com/contest/1136/problem/B 题意: 有n个井盖,每个井盖上有一个小石头. 给出n和k,k表示刚开始在第k个井盖上方. 有三种操作, ...

  5. CF 1136B Nastya Is Playing Computer Games

    题目链接:codeforces.com/problemset/problem/1136/B 题目分析 首先,读完题目,看了是个B题,嗯嗯...... 果断找规律,然后交了一波,居然过了!!! 代码区 ...

  6. Games on a CD

    Games on a CD time limit per test 4 seconds memory limit per test 512 megabytes input standard input ...

  7. Networked Graphics: Building Networked Games and Virtual Environments (Anthony Steed / Manuel Fradinho Oliveira 著)

    PART I GROUNDWORK CHAPTER 1 Introduction CHAPTER 2 One on One (101) CHAPTER 3 Overview of the Intern ...

  8. Codeforces Beta Round #37 B. Computer Game 暴力 贪心

    B. Computer Game 题目连接: http://www.codeforces.com/contest/37/problem/B Description Vasya's elder brot ...

  9. Mesa (computer graphics)

    http://en.wikipedia.org/wiki/Mesa_(computer_graphics) Mesa (computer graphics) From Wikipedia, the f ...

随机推荐

  1. 8个不可不知的Mac OS X专用命令行工具【转】

    OS X的终端下通用很多Unix的工具和脚本.如果从Linux迁移到OS X会发现很多熟悉的命令和脚本工具,其实并没有任何区别. 但是OS X也提供了很多其他系统所没有的特别的命令行工具.我们推荐8个 ...

  2. Ubuntu环境openresty的安装

    Ubuntu环境openresty的安装 相关库的安装 安装openresty需要的库  apt-get install libreadline-dev libncurses5-dev libpcre ...

  3. ECStore图片存储采用阿里云OSS(图片存储)服务

    主要功能:ECStore图片存储采用阿里云OSS(图片存储)服务   适用版本:ECStore 授权方式:授权域名使用,付费插件 联系方式: QQ 275553385  mail: jimingson ...

  4. PyCharm基本使用

    调节PyCharm的背景颜色 File>Settings>Appearance&Behavior>Appearance 在PyCharm中切换Python解释器版本 File ...

  5. jsp:useBean的使用

    ->Bean的基本要素: 1.必须要有一个不带参数的构造器,在jsp元素创建Bean时会调用空构造器 2.Bean类应该没有任何公共实例变量,也就是说,不允许直接访问实例变量,通过setter/ ...

  6. nyoj 523 双向广搜

    题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=523 #include<iostream> #include<cstd ...

  7. 5、范围标签<fieldset></fieldset>

    <fieldset style="border:0;border:1px solid red;"> <legend style="background- ...

  8. ACM网络镜像赛

    参加了东大,南理的网络镜像赛,分别答对了两道题和三道题.感觉自己完全被虐了.发现了很多自己应该去掌握的知识,找到了自己的差距.容斥原理,博弈论,等等,这将是我努力的方向. 在跟大神同学和学姐交流的过程 ...

  9. Tiny6410之按键裸机驱动

    操作步骤: 第一步:查看开发板电路原理图 找到LED 和按键的管脚所对应的寄存器 LED:(见Tiny6410之LED裸机驱动) nLED_1 - GPK4 nLED_2 - GPK5 nLED_3 ...

  10. 兼容不同浏览器的 CSS Hack 写法

    所谓 CSS Hack,是指在 CSS 代码中嵌入诸如 *,*html  等代码,方便于独立控制某种浏览器的具体样式.比如有些 CSS Hack 只能被 IE6 或 IE7 识别,而 Firefox ...