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):

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL; const int MAXN = ; void getFail(char *P, int f[]) {
int m = strlen(P);
f[] = f[] = ;
for(int i = ; i < m; ++i) {
int j = f[i];
while(j && P[i] != P[j]) j = f[j];
f[i + ] = (P[i] == P[j] ? j + : );
}
} int KMP(char *T, char *P, int f[]) {
int n = strlen(T), m = strlen(P);
getFail(P, f);
int j = ;
for(int i = ; i < n; ++i) {
while(j && P[j] != T[i]) j = f[j];
if(P[j] == T[i]) ++j;
if(j == m) return i - m + ;
}
return -;
} char s1[MAXN], s2[MAXN], ans[MAXN];
char tmp[MAXN];
int f[MAXN], ans_cnt; bool get_next(char *&src, char *ret) {
while(*src == ' ') ++src;
if(*src == ) return false;
int i = ;
while(*src != ' ' && *src != ) ret[i++] = *src, ++src;
ret[i] = ;
return true;
} bool solve() {
int now = ;
ans_cnt = ;
char *ss = s2;
while(get_next(ss, tmp)) {
int pos = KMP(s1 + now, tmp, f);
if(pos == -) return false;
//cout<<pos<<endl;
for(int i = now; i < now + pos; ++i) {
ans[ans_cnt++] = (s1[i] == ' ' ? ' ' : '_');
}
now += pos;
for(int i = ; tmp[i]; ++i, ++now) ans[ans_cnt++] = tmp[i];
//while(s1[now] != ' ' && s1[now] != 0) ans[ans_cnt++] = '_', ++now;
if(s1[now]) {
if(s1[now] != ' ') ans[ans_cnt++] = '_';
else ans[ans_cnt++] = ' ';
++now;
} }
for(int i = now; s1[i]; ++i) {
ans[ans_cnt++] = (s1[i] == ' ' ? ' ' : '_');
}
ans[ans_cnt++] = ;
return true;
} int main() {
gets(s1);
gets(s2);
if(!solve()) puts("I HAVE FAILED!!!");
else printf("%s\n", ans);
}

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. Python基础—06-函数基础

    函数基础 函数简介 定义:就是具有特定功能的一段代码 优点: 解决代码的重复书写 可以将功能的实现着和使用者分开,提高开发效率 分类: 库函数:print.input.abs等 自定义:用户自己封装的 ...

  2. React Native 中组件的生命周期(转)

    概述 就像 Android 开发中的 View 一样,React Native(RN) 中的组件也有生命周期(Lifecycle).所谓生命周期,就是一个对象从开始生成到最后消亡所经历的状态,理解生命 ...

  3. socket手写一个简单的web服务端

    直接进入正题吧,下面的代码都是我在pycharm中写好,再粘贴上来的 import socket server = socket.socket() server.bind(('127.0.0.1', ...

  4. php第三节(运算符)

    <?php //算术运算符 + - * / % //++ 前加加 先做加运算后座赋值运算 后加加 先做赋值运算后座加运算 //-- 前减减 先做加运算后座赋值运算 后减减 先做赋值运算后座加运算 ...

  5. #leetcode刷题之路4-寻找两个有序数组的中位数

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2.请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)).你可以假设 nums1 和 nums2 不会 ...

  6. 『ACM C++』PTA浙大 | 基础题 - 打印沙漏

    <数据结构>开课前的一些小作业练习,可能因为一个寒假都没有打C++手生了,整个寒假都在帮拍电影做后期特效,导致这道题居然用了两个钟去AC,深感惭愧,作个标记吧,下面上题. 一首好曲推荐:同 ...

  7. Django模板简介

    在settings.py中有个TEMPLATES的设置,其中BACKEND用来配置Django模板引擎, DIRS 定义了一个目录列表,模板引擎按列表顺序搜索这些目录以查找模板源文件 一般我们都会把模 ...

  8. For macOS.百度网盘 破解SVIP、下载速度限制~

    For macOS.百度网盘 破解SVIP.下载速度限制~ 是插件的 https://github.com/CodeTips/BaiduNetdiskPlugin-macOS 2019-01-03 让 ...

  9. Struts2拦截器说明

    有关于Struts2的拦截器的原理 在此共设置了两个拦截器,firstInterception.SecondInterception package struts2_inteception; publ ...

  10. python爬虫 爬取steam热销游戏

    好久没更新了啊...最近超忙 这学期学了学python 感觉很有趣 就写着玩~~~ 爬取的页面是:https://store.steampowered.com/search/?filter=globa ...