#sicily#1082. Computer Game

1 篇文章 0 订阅

题目来源:http://soj.sysu.edu.cn/1082

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
integer1 integer2 … integercode_dimension
protection_dimension
integer1 integer2 … 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
Copy sample input to clipboard
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

Problem Source: ZSUACM Team Member

#include <cstdio>
#include <stdio.h>
#include <cstring>

int arr[60010];
int next[60010];
int pro[1000010];
using namespace std;

int main() {
    int lenOfCode = 0;
    int lenOfProtection = 0;
    while (scanf("%d", &lenOfCode) != EOF) {
          if (0 != lenOfCode) {
             for (int i = 0; i < lenOfCode; i++) {
                 scanf("%d", &arr[i]);
             }

              scanf("%d", &lenOfProtection);
              for(int i = 0; i < lenOfProtection; i++) {
                 scanf("%d", &pro[i]);
              }

              memset(next, 0, sizeof(next[0]) * 60010);
              next[0] = 0;
              int index = 0;
              for (int i = 1; i < lenOfCode; i++) {
                if (arr[i] == arr[index]) {
                    next[i] = index;
                    index++;
                } else {
                    next[i] = index;
                    index = 0;
                }
              }

              index = 0;
              int result = 0;
              bool found = false;
              for (int i = 0; i < lenOfProtection ; i++) {

                        if (arr[index] == pro[i]) {
                            index++;
                        } else if (index != 0){
                            i--;
                            index = next[index];
                        }

                    if (index == lenOfCode) {
                        result = i - index + 1;
                        found = true;
                        break;
                    }                   
              }
              if (found) printf("%d\n", result);
              else printf("no solution\n");
          } 
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值