模式串为子串

KMP

/*

@author : victor

*/

#include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int MAX = 5e6 + ; int next_[MAX];
char s1[MAX], s2[MAX], ans[MAX];
int pos[MAX];
int l1, l2, cnt; void get_next(char x[],int m,int next_[])
{
int i ,j;
j = next_[] = -;
i = ;
while(i < m){
while(-!=j&&x[i]!=x[j]) j = next_[j];
next_[++i] = ++j;
}
} void KMP(){
int i = , j = ;
cnt = ;
while(s1[i] != '\0')
{
ans[cnt] = s1[i ++];
while(!(j == - || ans[cnt] == s2[j]))
j = next_[j];
j ++;
cnt ++;
pos[cnt] = j;
if(j >= l2)
{
cnt -= l2;
j = pos[cnt];
}
}
} int main()
{
while(scanf("%s %s", s2, s1) != EOF)
{
l1 = strlen(s1);
l2 = strlen(s2);
get_next(s2,l2,next_);
KMP();
for(int i = ; i < cnt; i++)
printf("%c", ans[i]);
printf("\n");
}
} // A B C A B C A B C
// -1 0 0 0 1 2 3 4 5 6
// A B C B C C B A C

2015 四川省赛 C Censor(哈希 | KMP)的更多相关文章

  1. 2015四川省赛 D Vertex Cover 搜索

    题意: 给出一个\(n\)个点\(m\)条边的无向图,现在要给若干个点染色,使得每条边都至少邻接一个被染色的顶点.问至少要给多少各点染色才能满足条件. 分析: 注意到题目中有一个很特殊的条件: 对于图 ...

  2. hdu5442(2015长春赛区网络赛1006)后缀数组+KMP /最小表示法?

    题意:给定一个由小写字母组成的长度为 n 的字符串,首尾相连,可以从任意一个字符开始,顺时针或逆时针取这个串(长度为 n),求一个字典序最大的字符串的开始字符位置和顺时针或逆时针.如果有多个字典序最大 ...

  3. SCU 4440 Rectangle 2015年四川省赛题

    题目链接:http://acm.scu.edu.cn/soj/problem/4440/ 题目大意:给一个n*m的方格,求周长小于等于k的矩形有多少个. 解题思路:我之前直接暴力,显然超时,所以后来发 ...

  4. SCU 4436 Easy Math 2015年四川省赛题

    题目链接:http://acm.scu.edu.cn/soj/problem/4436/ 题意:给你n个整数,求这n个数的平方根和是否是一个整数: 解题思路:如果这题每个数给他算出来,必然费时间,可能 ...

  5. UOJ Round #15 [构造 | 计数 | 异或哈希 kmp]

    UOJ Round #15 大部分题目没有AC,我只是水一下部分分的题解... 225[UR #15]奥林匹克五子棋 题意:在n*m的棋盘上构造k子棋的平局 题解: 玩一下发现k=1, k=2无解,然 ...

  6. Codeforces 109D String Transformation 字符串 哈希 KMP

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF109D.html 题目传送门 - CF109D 题意 给定两个字符串 $a,b$ ,求一组 $i,j$ 使得 ...

  7. 四川省赛 SCU - 4438

    Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text pp. Her j ...

  8. 7.26机房报零赛——无尽的矩阵【kmp+hash】

    恩,其实大家都没有报零,反正我是蒟蒻 为了纪念我第一次打过哈希,特此写一篇题解 题目描述 从前有一个的小矩阵,矩阵的每个元素是一个字母(区分大小写),突然有一天它发生了 变异,覆盖了整个二维空间,即不 ...

  9. NOI 2015 滞后赛解题报告

    报同步赛的时候出了些意外.于是仅仅能做一做"滞后赛"了2333 DAY1 T1离线+离散化搞,对于相等的部分直接并查集,不等部分查看是否在同一并查集中就可以,code: #incl ...

随机推荐

  1. You Are Given a WASD-string...

    C. You Are Given a WASD-string... 主要看的还是思维,分别求出在上下左右四个方向移动的最大幅度( mov_up, mov_down, mov_right, mov_le ...

  2. hive单用户多点模式配置

    简介 单用户多点模式也称远程服务模式,用户非java客户端访问元数据库,在服务端启动MetaStoreServer,客户端利用Thrift协议通过MetaStoreServer访问元数据库. mysq ...

  3. What’s up with the Graph Laplacian

    What's up with the Graph Laplacian? 来源 作者:Jeremy Kun blog: Math ∩ Programming 在数学上图和与图关联的某些矩阵的代数性质有很 ...

  4. C++入门经典-例5.18-通过引用交换数值

    1:在C++中,函数参数的传递方式主要有两种,即值传递和引用传递.值传递是指在函数调用时,将实际参数的值赋值一份传递到调用函数中,这样如果在调用函数中修改了参数的值,其改变将不会影响到实际参数的值.而 ...

  5. spring的IOC——依赖注入的两种实现类型

    一.构造器注入: 构造器注入,即通过构造函数完成依赖关系的设定.我们看一下spring的配置文件: <constructor-arg ref="userDao4Oracle" ...

  6. C# 下载文件的心得

    下载文件最常用的有两种: 第一种:直接使用A标签,对应着文件的地址. 第二种:将文件写成流,然后在回传给客户端. 第一种,使用起来方便,但是有个坏处,如果你的文件是可编辑的文件(比如Excel wor ...

  7. leetcode-easy-others-20 Valid Parentheses

    mycode   95.76% class Solution(object): def isValid(self, s): """ :type s: str :rtype ...

  8. @RequestHeader和@CookieValue的使用

    /** * 了解: * * @CookieValue: 映射一个 Cookie 值. 属性同 @RequestParam */ @RequestMapping("/testCookieVal ...

  9. 【转】How-to: Enable User Authentication and Authorization in Apache HBase

    With the default Apache HBase configuration, everyone is allowed to read from and write to all table ...

  10. CompletableFuture用法介绍

    一.CompletableFuture用法入门介绍 入门介绍的一个例子: package com.cy.java8; import java.util.Random; import java.util ...