zoukankan      html  css  js  c++  java
  • URAL 1732 Ministry of Truth(KMP)

    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 = 100010;
    10 
    11 void getFail(char *P, int f[]) {
    12     int m = strlen(P);
    13     f[0] = f[1] = 0;
    14     for(int i = 1; i < m; ++i) {
    15         int j = f[i];
    16         while(j && P[i] != P[j]) j = f[j];
    17         f[i + 1] = (P[i] == P[j] ? j + 1 : 0);
    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 = 0;
    25     for(int i = 0; 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 + 1;
    29     }
    30     return -1;
    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 == 0) return false;
    40     int i = 0;
    41     while(*src != ' ' && *src != 0) ret[i++] = *src, ++src;
    42     ret[i] = 0;
    43     return true;
    44 }
    45 
    46 bool solve() {
    47     int now = 0;
    48     ans_cnt = 0;
    49     char *ss = s2;
    50     while(get_next(ss, tmp)) {
    51         int pos = KMP(s1 + now, tmp, f);
    52         if(pos == -1) 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 = 0; 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++] = 0;
    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
    ", ans);
    79 }
    View Code
  • 相关阅读:
    【python】超有用的集合类collections,不来了解一下?
    【pytest】teardown里的yield和addfinalizer
    听过N次还是不会之:浏览器输入url后到底经历了什么
    web-UI自动化必会技能—xpath轴,了解一下?
    想在java接口自动化里用上Python的requests?这样做就可以了
    web自动化多次打开浏览器嫌烦?打开一次浏览器,pytest有个招
    【web系统UI自动化】关于UI自动化的总结
    【接口自动化】selenium库也有大用场(获取cookie)
    【python开发】迈出第一步,这可能是我唯一一次的Python开发了
    angular9的学习(八)组件的内置api
  • 原文地址:https://www.cnblogs.com/oyking/p/3290150.html
Copyright © 2011-2022 走看看