zoukankan      html  css  js  c++  java
  • bzoj3942

    有一个S串和一个T串,长度均小于1,000,000,设当前串为U串,然后从前往后枚举S串一个字符一个字符往U串里添加,若U串后缀为T,则去掉这个后缀继续流程。
    将s中的每一个字符压入栈
    暴力将s中的字符和t中的一个一个匹配,若能够匹配
    若不能匹配,我们利用next数组转移匹配的位置
    同时我们利用另一个栈来存储匹配到的位置,这意味着我们的两个栈时同时进栈同时出栈的
    当我们匹配到的长度为t的长度时,我们将栈中的"t"出栈
    如此进行
    最后输出栈中剩余字符
    口胡有点说不清,建议看代码

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 1000086;
     4 char s[maxn], t[maxn];
     5 int Next[maxn];
     6 int s2[maxn], top = 0;
     7 char s1[maxn]; 
     8 int lens, lent;
     9 
    10 inline void KMP_T() {
    11     Next[1] = 0;
    12     for(int i = 2, j = 0; i <= lent; ++i) {
    13         while(j > 0 && t[i] != t[j + 1]) j = Next[j];
    14         if(t[i] == t[j + 1]) j++;
    15         Next[i] = j;
    16     }
    17     /*for(int i = 1; i <= lent; ++i)
    18         cout << Next[i] << ' ';
    19     cout << '
    ';*/
    20 }
    21 
    22 int main() {
    23     cin >> s + 1 >> t + 1;
    24     lens = strlen(s + 1);
    25     lent = strlen(t + 1);
    26     KMP_T();
    27     for(int i = 1; i <= lens; ++i) {
    28         int j = s2[top];
    29         s1[++top] = s[i];
    30         while(j != 0 && s1[top] != t[j + 1]) j = Next[j];
    31         if(s1[top] == t[j + 1]) j++;
    32         s2[top] = j;
    33         if(s2[top] == lent) top -= lent;
    34     }
    35     for(int i = 1; i <= top; ++i)
    36         cout << s1[i];
    37     cout << '
    ';
    38     return 0;
    39 } 
    View Code
  • 相关阅读:
    软件工程概论通读第二章
    软件工程概论通读第一章
    mac 下安装mongodb
    angular5 ng-content使用方法
    angular5 @viewChild @ContentChild ElementRef renderer2
    关于日期的一篇很好的文章
    angular5 组件之间监听传值变化
    angular5 ng-bootstrap和ngx-bootstrap区别
    angular5表单验证问题
    angular5 路由变化监听
  • 原文地址:https://www.cnblogs.com/ywjblog/p/9275126.html
Copyright © 2011-2022 走看看