HDU 5007 Post Robot KMP (ICPC西安赛区网络预选赛 1001)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5007

解题报告:输入一篇文章,从头开始,当遇到 “Apple”, “iPhone”, “iPod”, “iPad” 这几个字符串时,输出”MAI MAI MAI!“,当遇到"Sony"时,输出“SONY DAFA IS GOOD!"。

看题太渣,题目意思还有个每个单词最多只出现一次关键词。有了这个条件就简单了,只要分别对每个单词进行五次KMP,匹配到了就输出对应的解释。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 char str[1000000];
 7 int next[1000000];
 8 void get_next(char* s,int* next)
 9 {
10     int len = strlen(s);
11     int i = -1,j = 0;
12     memset(next,0,sizeof(next));
13     next[0] = 0;
14     while(j < len)
15     {
16         if(i < 0 || s[i] == s[j])
17         {
18             i++;
19             next[j+1] = i;
20         }
21         else i = 0;
22         j++;
23     }
24 }
25 int kmp(char* s,char* t,int* next)
26 {
27     get_next(t,next);
28     int lens = strlen(s);
29     int lent = strlen(t);
30     int i = 0,j = 0;
31     while(i < lens && j < lent)
32     {
33         if(s[i] == t[j]) j++;
34         else j = next[j];
35         i++;
36     //    printf("%d %d\n",i,j);
37     }
38     if(j == lent) return i - lent;  //匹配成功返回开始配对的位置
39     else return -1;   //匹配失败,返回尾指针
40 }
41 char temp[6][20] = {"Apple","iPhone","iPod","iPad","Sony"};
42 int main()
43 {
44     while(scanf("%s",str)!=EOF)
45     {
46         if(kmp(str,temp[0],next)!=-1) puts("MAI MAI MAI!");
47         if(kmp(str,temp[1],next)!=-1) puts("MAI MAI MAI!");
48         if(kmp(str,temp[2],next)!=-1) puts("MAI MAI MAI!");
49         if(kmp(str,temp[3],next)!=-1) puts("MAI MAI MAI!");
50         if(kmp(str,temp[4],next)!=-1) puts("SONY DAFA IS GOOD!");
51     }
52 }

时间: 2024-08-08 13:47:16

HDU 5007 Post Robot KMP (ICPC西安赛区网络预选赛 1001)的相关文章

ACM-ICPC 2018南京赛区网络预选赛

A题:An Olympian Math Problem 可以发现最终的答案就是n-1 1 #include <iostream> 2 #include<bits/stdc++.h> 3 using namespace std; 4 typedef long long ll; 5 int main() 6 { 7 int t; 8 ll n; 9 scanf("%d",&t); 10 while(t--) 11 { 12 scanf("%lld&

HDU 5007 Post Robot(字符串寻找)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5007 Problem Description DT is a big fan of digital products. He writes posts about technological products almost everyday in his blog. But there is such few comments of his posts that he feels depressed

HDU 5007 Post Robot

Post Robot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1306    Accepted Submission(s): 941 Problem Description DT is a big fan of digital products. He writes posts about technological produc

hdu 5956 The Elder 2016ACM/ICPC沈阳赛区现场赛I

Problem Description Once upon a time, in the mystical continent, there is a frog kingdom, ruled by the oldest frog, the Elder. The kingdom consists of N cities, numbered from east to west. The 1-th city, which is located to the east of others, is the

HDU 5884 Sort -2016 ICPC 青岛赛区网络赛

题目链接 #include <iostream> #include <math.h> #include <stdio.h> #include<algorithm> #include<cstring> #include<queue> using namespace std; int data[100005]; int main() { int T,n,t; scanf("%d",&T); while(T--)

HDU 5878 I Count Two Three (打表+二分查找) -2016 ICPC 青岛赛区网络赛

题目链接 题意:给定一个数n,求大于n的第一个只包含2357四个因子的数(但是不能不包含其中任意一种),求这个数. 题解:打表+二分即可. #include <iostream> #include <math.h> #include <stdio.h> #include<algorithm> using namespace std; long long data[1000000],tot=0; int main() { long long maxn = 10

HDU 5875 Function -2016 ICPC 大连赛区网络赛

题目链接 网络赛的水实在太深,这场居然没出线zzz,差了一点点,看到这道题的的时候就剩半个小时了.上面是官方的题意题解,打完了才知道暴力就可以过,暴力我们当时是想出来了的,如果稍稍再优化一下估计就过了zzz.去年有一场现场赛也是n=1000,n^3过了,看来关键时刻实在做不出来就得大胆暴力啊. #include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+5; int a[maxn]

HDU 5879 Cure -2016 ICPC 青岛赛区网络赛

题目链接 题意:给定一个数n,求1到n中的每一项的平方分之一的累加和. 题解:题目没有给数据范围,而实际上n很大很大超过long long.因为题目只要求输出五位小数,我们发现当数大到一定程度时值是固定的 pi*pi/6.小的打表就行了,这里打表为了防止爆内存我用了优化的方法,类似于我之前写的light oj 1234. #include <iostream> #include <math.h> #include <stdio.h> #include <cstri

HDU 5881 Tea -2016 ICPC 青岛赛区网络赛

题目链接 题意:有一壶水, 体积在 L和 R之间, 有两个杯子, 你要把水倒到两个杯子里面, 使得杯子水体积几乎相同(体积的差值小于等于1), 并且使得壶里剩下水体积不大于1. 你无法测量壶里剩下水的体积, 问最小需要倒水的次数. 题解:考虑倒水的大致过程,L = 0 和 L = 1 的情况应该是等价的,所以不妨设 L > 0.首先向一个杯子倒 L/2 升水,再往另一个杯子倒  L/2+1 升水.接下来就来回往两个杯子里倒 2 升,直到倒空为止.这样就很容易分析出需要倒水的次数.唯一注意的是最后