Sicily 1282. Computer Game

题目地址:1282. Computer Game

思路:

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

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

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

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

这道题具体代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4
 5 int main() {
 6     int len1, len2;
 7     while (cin >> len1) {
 8         int code[60001] = {0};
 9         int next[60001] = {0};
10         for (int i = 1; i <= len1; i++) {
11             scanf("%d", &code[i]);
12         }
13         int index = 0;
14         for (int i = 2; i <= len1; i++) {
15             index = code[index+1] == code[i] ? index+1 : 0;
16             next[i] = index;
17         }
18         cin >> len2;
19         index = 0;
20         int test, result;
21         for (int i = 1; i <= len2; i++) {
22             scanf("%d", &test);
23             if (index != len1) {
24                 index = code[index+1] == test ? index+1 : next[index];
25                 if (index == len1) {
26                     result = i-len1;
27                 }
28             }
29         }
30         index == len1 ? cout << result << endl : cout << "no solution\n";
31     }
32
33     return 0;
34 } 
时间: 2024-11-03 21:53:33

Sicily 1282. Computer Game的相关文章

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

the major advances since the birth of the computer

COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION ? The family concept: Introduced by IBM with its System/360 in 1964, followed shortly thereafter by DEC, with its PDP-8. The family concept decouples the architecture of a

UVAOJ 12096 The SetStack Computer(STL的运用)

12096 The SetStack Computer Background from Wikipedia: Set theory is a branch ofmathematics created principally by the German mathe-matician Georg Cantor at the end of the 19th century. Initially controversial, set theory has come to play therole of

poj3436--ACM Computer Factory(最大流,拆点dinic)

ACM Computer Factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5501   Accepted: 1887   Special Judge Description As you know, all the computers used for ACM contests must be identical, so the participants compete on equal terms. Th

The Security Database on the Server Does Not Have a Computer Account

这两天在做微软App-V应用程序虚拟化的试验,公司需要测试自有C/S架构产品在其上的部署. 搭建过程比较顺利,突然的今天上班后发现App-V Server(域成员服务器)使用域用户登陆时报错: The Security Database on the Server Does Not Have a Computer Account for This Workstation Trust Relationship Google到该问题的解决方法,说不上准确的道理来,比较"有趣",记录下. 微

Sicily 1034. Forest

1034. Forest Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description In the field of computer science, forest is important and deeply researched , it is a model for many data structures . Now it’s your job here to calculate the depth and widt

【树形dp】Computer

Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7324    Accepted Submission(s): 3627 Problem Description A school bought the first computer some time ago(so this computer's id is 1). Du

hdu 5154 Harry and Magical Computer 拓扑排序

Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description In reward of being yearly outstanding magic student, Harry gets a magical computer. When the computer begins to deal wi

computer systems work from a programmer&#39;s perspective

2. typedef unsigned char *byte_pointer; void show_bytes(byte_pointer start, size_t len) { int i; for (i=0;i<len;i++) printf(" %.2x",start[i]; printf("\n"); } void inplace_swap(int *x,int *y) { *y = *x^*y; *x = *x^*y; *y = *x^*y; } c