Description

In whiteblack on blackwhite is written the utterance that has been censored by the Ministry of Truth. Its author has already disappeared along with his whole history, and now, while Big Brother is watching somebody else, you, as an ordinary official of the Minitrue, have to delete some letters from the utterance so that another utterance will appear, which has been approved of by the Ministry.
The Ministry of Truth defines a word as a nonempty sequence of English letters and an utterance as a sequence of one or more words separated with one or more spaces. There can also be spaces before the first word and after the last word of an utterance. In order to compare two utterances, one should delete all the leading and trailing spaces and replace each block of consecutive spaces with one space. If the resulting strings coincide, then the utterances are considered to be equal. When the official deletes a letter from the utterance, this letter turns into a space.

Input

The first line contains the original utterance and the second line contains the utterance that must be obtained. The length of each utterance is at most 100000 symbols. The words in both utterances are separated with exactly one space; there are no leading or trailing spaces in each line. The original and the required utterances are different.

Output

If you can't carry out your order, output “I HAVE FAILED!!!” in the only line. Otherwise, output the original utterance replacing the letters that are to be deleted with the underscore character.

题目大意:给两行字符串,可以删掉上面的字符串的某些字符,删掉的字符变成空格,删完后能否变成下面的字符串。若能则输出上面的字符串,其中删掉的字符变成下划线。

思路:KMP给下面的每一个单词匹配上面的字符串就行了,注意细节处理。区分大小写。

PS:输入没有多余空格不要误会了。

PS2:之前误解了题意所以可能代码会有点奇葩……

代码(281MS):

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <queue>
  6. using namespace std;
  7. typedef long long LL;
  8.  
  9. const int MAXN = ;
  10.  
  11. void getFail(char *P, int f[]) {
  12. int m = strlen(P);
  13. f[] = f[] = ;
  14. for(int i = ; i < m; ++i) {
  15. int j = f[i];
  16. while(j && P[i] != P[j]) j = f[j];
  17. f[i + ] = (P[i] == P[j] ? j + : );
  18. }
  19. }
  20.  
  21. int KMP(char *T, char *P, int f[]) {
  22. int n = strlen(T), m = strlen(P);
  23. getFail(P, f);
  24. int j = ;
  25. for(int i = ; i < n; ++i) {
  26. while(j && P[j] != T[i]) j = f[j];
  27. if(P[j] == T[i]) ++j;
  28. if(j == m) return i - m + ;
  29. }
  30. return -;
  31. }
  32.  
  33. char s1[MAXN], s2[MAXN], ans[MAXN];
  34. char tmp[MAXN];
  35. int f[MAXN], ans_cnt;
  36.  
  37. bool get_next(char *&src, char *ret) {
  38. while(*src == ' ') ++src;
  39. if(*src == ) return false;
  40. int i = ;
  41. while(*src != ' ' && *src != ) ret[i++] = *src, ++src;
  42. ret[i] = ;
  43. return true;
  44. }
  45.  
  46. bool solve() {
  47. int now = ;
  48. ans_cnt = ;
  49. char *ss = s2;
  50. while(get_next(ss, tmp)) {
  51. int pos = KMP(s1 + now, tmp, f);
  52. if(pos == -) return false;
  53. //cout<<pos<<endl;
  54. for(int i = now; i < now + pos; ++i) {
  55. ans[ans_cnt++] = (s1[i] == ' ' ? ' ' : '_');
  56. }
  57. now += pos;
  58. for(int i = ; tmp[i]; ++i, ++now) ans[ans_cnt++] = tmp[i];
  59. //while(s1[now] != ' ' && s1[now] != 0) ans[ans_cnt++] = '_', ++now;
  60. if(s1[now]) {
  61. if(s1[now] != ' ') ans[ans_cnt++] = '_';
  62. else ans[ans_cnt++] = ' ';
  63. ++now;
  64. }
  65.  
  66. }
  67. for(int i = now; s1[i]; ++i) {
  68. ans[ans_cnt++] = (s1[i] == ' ' ? ' ' : '_');
  69. }
  70. ans[ans_cnt++] = ;
  71. return true;
  72. }
  73.  
  74. int main() {
  75. gets(s1);
  76. gets(s2);
  77. if(!solve()) puts("I HAVE FAILED!!!");
  78. else printf("%s\n", ans);
  79. }

URAL 1732 Ministry of Truth(KMP)的更多相关文章

  1. URAL 1732. Ministry of Truth ( KMP 多模式串匹配 )

    问在第一个串中删掉几个字符能否得到第二个串.注意在第二个串中不连续的单词在第一个串中也必须不连续. 一组数据: Input: abababbbbababbb aba ab Output: I HAVE ...

  2. poj2406 Power Strings(kmp)

    poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...

  3. POJ 2406 Power Strings(KMP)

    Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...

  4. LightOJ 1258 Making Huge Palindromes(KMP)

    题意 给定一个字符串 \(S\) ,一次操作可以在这个字符串的右边增加任意一个字符.求操作之后的最短字符串,满足操作结束后的字符串是回文. \(1 \leq |S| \leq 10^6\) 思路 \( ...

  5. codeM编程大赛E题 (暴力+字符串匹配(kmp))

    题目大意:S(n,k)用k(2-16)进制表示1-n的数字所组成的字符串,例如S(16,16)=123456789ABCDEF10: 解题思路: n最大50000,k最大100000,以为暴力会超时. ...

  6. 经典串匹配算法(KMP)解析

    一.问题重述 现有字符串S1,求S1中与字符串S2完全匹配的部分,例如: S1 = "ababaababc" S2 = "ababc" 那么得到匹配的结果是5( ...

  7. Leetcode28--->字符串的匹配(KMP)

    题目: 题目的本质是给定两个字符串str1,str2,求str1中的str2串开始的地方,即字符串的匹配,KMP算法 思路:时间复杂度为O(m + n),空间复杂度为O(n),原串的长度为m,子串的长 ...

  8. 题解0012:剪花布条(KMP)

    信奥一本通1465 KPM例题 题目链接:http://ybt.ssoier.cn:8088/problem_show.php?pid=1465 题目描述:给出花布条和小饰条(字符串),求花布条中能剪 ...

  9. BZOJ 3796 Mushroom追妹纸 哈希+二分(+KMP)

    先把两个串能匹配模式串的位置找出来,然后标记为$1$(标记在开头或末尾都行),然后对标记数组求一个前缀和,这样可以快速查到区间内是否有完整的一个模式串. 然后二分子串(答案)的长度,每次把长度为$md ...

随机推荐

  1. mysql substring_index()查询某个字符中以某个分割符分割后的值

    substring_index(某个字段,以其分割,第几个分割点之前的值); +---------------------------------------------------------+ | ...

  2. 利用css transition属性实现一个带动画显隐的微信小程序部件

    我们先来看效果图 像这样的一个带过渡效果的小部件在我们实际开发中的应用几率还是比较大的,但是在开发微信小程序的过程中可能有的小伙伴发现transition这个属性它不好使(下面说明)所以我们这个时候会 ...

  3. 原 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)(Finchley版本)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f8-bus/ 本文出自方志朋的博客 转载请标明出处: Spr ...

  4. window与MAC,多台机器ssh免密码登录同一台机器执行某个脚本,

    参考:https://zhidao.baidu.com/question/586579720.html A B C三台机器上以当前用户运行如下命令生成本主机的公钥和私钥文件: 1 ssh-keygen ...

  5. JSP静态包含和动态包含

    JSP中有两种包含: 静态包含:<%@include file="被包含页面"%>: 动态包含:<jsp:include page="被包含页面&quo ...

  6. 你不知道的javaScript笔记(4)

    类型: JavaScript 有7种内置类型 空值 (null) 未定义(undefined) 布尔值(boolean) 数字(number) 字符串(string) 对象(object) 符号(sy ...

  7. CF605A Sorting Railway Cars(递推)

    题目描述 An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers ...

  8. 2018 Wannafly summer camp Day3--Shopping

    Shopping 描述 题目描述: 你要买n件物品,其中有一些是凳子. 商场正在举行促销活动,如果购物车中有至少一个凳子,那么你可以半价购买这个购物车中最贵的一个物品. 你有m辆购物车,请最小化你的花 ...

  9. NPOI追加内容到xlsx报错,用Epplus往excle xlsx追加数据

    问题的现象就是,只要不修改xlsx,一直写入或者再次写入xlsx,追加内容都是不会有问题的.但是只要你修改了xlsx,用excle2010或者2007修改了xlsx的内容里面的列宽,或者行高,或者进行 ...

  10. 嵌入式Linux系统移植(二)——交叉编译工具集

    常用工具:readelf.size.nm.strip.strings.objdump.objcopy.addr2line readelf:读可执行文件的elf头 ELF Header: Magic: ...