URAL 1684. Jack's Last Word KMP

题目来源:URAL 1684. Jack‘s Last Word

题意:输入a b 把b分成若干段 每一段都是a的前缀

思路:b为主串 然后用a匹配b 记录到b的i位置最大匹配的长度 然后切割 切割的时候要从后往前

假设a = abac b = abab 那么假设从前往后 首先覆盖了aba 然后b就不能覆盖了 从后往前就能够了 首先覆盖ab 下一次还是ab

由于已经记录了到i位置的最大匹配长度 依据长度从末尾倒退 每次倒退的时候仅仅要是最大的匹配的长度

由于假设在某一次的递推 记录的最大匹配的前缀是x 那么这次应该倒退到i-x

假设不倒退x 倒退小于x的字符y 而且x是能够倒退 剩下的是y-x必然能够倒退 那么一次解决即可了

#include <cstdio>
#include <cstring>
const int maxn = 100010;
char a[maxn], b[maxn];
int f[maxn];
int dp[maxn];
char c[maxn*2];
void get_fail(char* s)
{
	f[0] = f[1] = 0;
	int n = strlen(s);
	for(int i = 1; i < n; i++)
	{
		int j = f[i];
		while(j && s[i] != s[j])
			j = f[j];
		if(s[i] == s[j])
			f[i+1] = j+1;
		else
			f[i+1] = 0;
	}
}
int main()
{
	while(scanf("%s %s", a, b) != EOF)
	{
		get_fail(a);
		int n = strlen(b), m = strlen(a);
		int j = 0;
		for(int i = 0; i < n; i++)
		{
			while(j && b[i] != a[j])
				j = f[j];
			if(a[j] == b[i])
				j++;
			dp[i] = j;
			if(j == m)
				j = f[j];			

		}
		c[n*2] = 0;
		int len = n*2, i;
		for(i = n-1; i >= 0; )
		{
			int k = dp[i];
			if(k == 0)
				break;

			for(int j = i; j > i-k; j--)
				c[--len] = a[j-i+k-1];
			c[--len] = ' ';
			i = i-k;
		}
		if(i != -1)
			puts("Yes");
		else
		{
			puts("No");
			puts(c+len+1);
		}
	}
	return 0;
}

URAL 1684. Jack's Last Word KMP

时间: 2024-10-11 10:42:30

URAL 1684. Jack&#39;s Last Word KMP的相关文章

URAL 1684. Jack&#39;s Last Word KMP

题目来源:URAL 1684. Jack's Last Word 题意:输入a b 把b分成若干段 每一段都是a的前缀 思路:b为主串 然后用a匹配b 记录到b的i位置最大匹配的长度 然后分割 分割的时候要从后往前 如果a = abac b = abab 那么如果从前往后 首先覆盖了aba 然后b就不能覆盖了 从后往前就可以了 首先覆盖ab 下一次还是ab 因为已经记录了到i位置的最大匹配长度 根据长度从末尾倒退 每次倒退的时候只要是最大的匹配的长度 因为如果在某一次的递推 记录的最大匹配的前缀

URAL 1727. Znaika&amp;#39;s Magic Numbers(数学 vector)

主题链接:http://acm.timus.ru/problem.aspx?space=1&num=1727 1727. Znaika's Magic Numbers Time limit: 0.5 second Memory limit: 64 MB Znaika has many interests. For example, now he is investigating the properties of number sets. Znaika writes down some set

URAL 1707. Hypnotoad&amp;#39;s Secret(树阵)

URAL 1707. Hypnotoad's Secret space=1&num=1707" target="_blank" style="">题目链接 题意:这题设置的恶心不能多说.构造点和矩形.大概就是问每一个矩形里面是否包括点 思路:树状数组.把点排序,按y轴,在按x轴.在按询问,这样每次遇到一个点就在对应的扫描线上加.遇到查询就询问出左边到这个点位置的,就能预处理出每一个点左下角包括的点的个数,然后每一个矩形再利用容斥原理去搞一下就

LeetCode OJ:Add and Search Word - Data structure design(增加以及搜索单词)

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter

HDU 2594 Simpsons’ Hidden Talents (字符串-KMP)

Simpsons' Hidden Talents Problem Description Homer: Marge, I just figured out a way to discover some of the talents we weren't aware we had. Marge: Yeah, what is it? Homer: Take me for example. I want to find out if I have a talent in politics, OK? M

[LeetCode][JavaScript]Add and Search Word - Data structure design

Add and Search Word - Data structure design Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or 

[LeetCode OJ] Word Search 深度优先搜索DFS

Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be us

[LintCode] Word Abbreviation Set

An abbreviation of a word follows the form . Below are some examples of word abbreviations: a) it --> it (no abbreviation) 1 b) d|o|g --> d1g 1 1 1 1---5----0----5--8 c) i|nternationalizatio|n --> i18n 1 1---5----0 d) l|ocalizatio|n --> l10n A

KMP算法应用举例

KMP是字符串匹配的经典算法 也是众多字符串基础的重中之重 A. 题意:给T组数据,每组有长度为n和m的母串和模式串.判断模式串是否是母串的子串,如果是输出最先匹配完成的位置,否则输出-1. 做法:直接套用模板.把char改成int.kmp函数中在模式串遍历到结尾的时候return,若没遍历到结尾,也就是不是子串返回-1 1 [cpp] view plain copy 2 #include <iostream> 3 #include <cstdio> 4 #include <