http://codeforces.com/problemset/problem/432/D 转自:https://blog.csdn.net/tc_to_top/article/details/38793973 题意 给出一个字符串,求有多少种长度的前缀和后缀相等,并且这种形式的子串在原字符串中出现的次数. 分析 首先得到主串的next数组,next数组的含义是next[j]的值表示str[0...j-1](我的next[0]是-1)这个子串的前后缀匹配的最长长度,如样例1 index  0…
题目连接:Codeforces 432D Prefixes and Suffixes 题目大意:给出一个字符串,求全部既是前缀串又是后缀串的字符串出现了几次. 解题思路:依据性质能够依据KMP算法求出全部的前后缀串,然后利用dp求解,dp[i]表示从1到i这个子串出现过的次数. 转移方程dp[jump[i]]+=dp[i]. 随意一个dp[i]的初始状态应该是1. #include <cstdio> #include <cstring> const int N = 1e5+5; i…
手动转田神的大作:http://blog.csdn.net/tc_to_top/article/details/38793973 D. Prefixes and Suffixes time limit per test 1:second memory limit per test: 256 megabytes input: standard input output: standard output You have a string s = s1s2...s|s|, where |s| is…
题目链接: https://codeforces.com/contest/432/problem/D 题解: 做法一: KMP 显然next树上\(n\)的所有祖先都是答案,出现次数为next树子树大小. 做法二: 后缀数组/Z-box 按照height分组,二分查找即可. 这种题经常KMP和Z-box都能做. 另外哪位神犇教我一下扩展KMP和Z-box有啥区别. 代码 KMP: #include<cstdio> #include<cstdlib> #include<cstr…
题目链接:http://codeforces.com/problemset/problem/432/D 题意: 给你一个字符串s,让你找出所有既是前缀又是后缀的子串,并输出它们分别出现了多少次. 题解: 先对原串求一次nex数组. 然后枚举位置i: sub(k)表示前缀s[0 to k] dp[i]表示sub(i)在原串s中的出现次数 假设nex[i] = a, nex[a] = b, nex[b] = c ... nex[z] = -1 则sub(a), sub(b), sub(c)...su…
由于包含了前缀与后缀,很容易想到用KMP去算前缀与后缀的公共缀.另外要计算某个后缀在整个串中出现的次数,由于后缀自动机是比较容易求的,然后就直接上后缀自动机了.先分别用KMP算法与后缀自动机跑一遍,然后对后缀自动机做一个拓扑排序,根据拓扑结构更新一遍每个串出现的次数就可以了.然后直接沿着KMP的next数组以及后缀自动机节点的父亲边走就可以了. 代码如下: #include <stdio.h> #include <string.h> ; struct sanode{ sanode…
D. Prefixes and Suffixes time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You have a string s = s1s2...s|s|, where |s| is the length of string s, and si its i-th character. Let's introduce s…
题目链接:Prefixes and Suffixes 题意:给定未知字符串长度n,给出2n-2个字符串,其中n-1个为未知字符串的前缀(n-1个字符串长度从1到n-1),另外n-1个为未知字符串的后缀(n-1个字符串长度从1到n-1),判断这2n-2个字符串分别为前缀还是后缀. 题解:从2n-2从找个n-1长度的字符串和1长度的字符串拼接,判断该字符串是否符合要求,能作为未知字符串.符合的话,直接输出答案. #include <set> #include <map> #includ…
题目大意: 求一个子串,子串既是前缀又是后缀. 然后再求出它在整个串中出现的次数. 思路分析: 能够非常easy想到怎样推断一个串既是前缀又是后缀. 仅仅须要它与 sa[0] 的lcp 等于 整个串的长度减去它的 sa 值. 然后接下来的工作是推断出现了 多少次. 首先我们想到,假设这个子串是目标前后缀. 那么出现过它的子串在sa 中的下标一定比这个串大. 由于它已经是最简的了. 然后能够直接二分求出他出现了多少次. #include <iostream> #include <cstdi…
题目大意 给你一个串 对于一个子串如果它既是前缀又是后缀 输出它的长度以及它在原串中一共出现了多少次 分析 对于既是前缀又是后缀的判断和126B相同 然后我们只需要记录每个不同的z[i]出现了多少次 然后对于每个合法z[i]输出所有大于z[i]的数的出现次数即可 因为如果长度为z[i]的前缀是最长前缀 那么小于z[i]的前缀一定合法 代码 #include<iostream> #include<cstdio> #include<cstring> #include<…
                                                    D. Prefixes and Suffixes You have a string s = s1s2...s|s|, where |s| is the length of string s, and si its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|)…
http://codeforces.com/contest/1092/problem/C Ivan wants to play a game with you. He picked some string ss of length nn consisting only of lowercase Latin letters. You don't know this string. Ivan has informed you about all its improper prefixes and s…
用扩展KMP做简单省力..... D. Prefixes and Suffixes time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You have a string s = s1s2...s|s|, where |s| is the length of string s, and si its i-th character.…
转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Prefixes and Suffixes You have a string s = s1s2...s|s|, where |s| is the length of string s, and si its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j…
CF432D Prefixes and Suffixes 题意 给你一个长度为n的长字符串,"完美子串"既是它的前缀也是它的后缀,求"完美子串"的个数且统计这些子串的在长字符串中出现的次数 分析 求出nex数组 , 在求出每个前缀出现的次数 , 从nex[n] 往下走就行了 其实这道题是 , KMP 求每个前缀出现次数的模板题 求前缀出现次数的写法 for(int i = 1 ; i <= n ; ++i) num[i]++; for(int i = n ;…
[Codeforces 1208D]Restore Permutation (树状数组) 题面 有一个长度为n的排列a.对于每个元素i,\(s_i\)表示\(\sum_{j=1,a_j<a_i}^i a_j\).即前面比它小的元素的值之和. 给出\(s_1,s_2 \dots s_n\),求a 分析 考虑如何求\(a_n\),\(s_n\)实际上表示的是1~n中比\(a_n\)小的所有数的和,可以直接求出\(a_n\) 然后我们可以倒序求\(a_i\),求到\(a_i\)的时候,我们已经知道\(…
题目链接 题意:给你一个长度n,还有2*n-2个字符串,长度相同的字符串一个数前缀一个是后缀,让你把每个串标一下是前缀还是后缀,输出任意解即可. 思路;因为不知道前缀还是后缀所以只能搜,但可以肯定的是长度为n-1的字符串一个是前缀一个是后缀,那么只要搜两次就完事了,一个当前缀不行就换另一个当前缀,然后中间判断一下即可. ps:这也是给远古题,没补,因为最不喜欢 字符串的题,,,qwq. #include<bits/stdc++.h> #define LL long long #define f…
题意:给你某个字符串的\(n-1\)个前缀和\(n-1\)个后缀,保证每个所给的前缀后缀长度从\([1,n-1]\)都有,问你所给的子串是前缀还是后缀. 题解:这题最关键的是那两个长度为\(n-1\)的子串,我们只要判断哪个是前缀就行了,然后再遍历一遍所给的子串,用长度为\(n-1\)的前缀子串来判断是子串是前缀还是后缀. 代码: int n; string s[N]; bool vis[N]; int cnt; int main() { ios::sync_with_stdio(false);…
Codeforces Round #246 (Div. 2) D:http://codeforces.com/contest/432/problem/D 题意:给你一个长度不超过10^5的字符串.要你按长度输出和后缀完全匹配的的前缀的长度.和该前缀在整个串中出现的次数.(可重叠) 题解:老实说,这一题,我完全是看样子看出规律出来的,想到用KMP,就是KMP数组反过来输出就可了,个数就是之前出现的次数加上本身的次数即可. 一下是别人的分析: 若第i个位置和第j个位置匹配了说明前缀j在第i个位置出现…
膜拜大佬:https://blog.csdn.net/xyz32768/article/details/88831233 题目链接: http://codeforces.com/contest/1139/problem/F 题意: 有n个物品,物品有三个属性分别是$p_i,s_i,b_i$ 有m个人,人有两个属性分别是$pref_j$,$inc_j$ 一个人能买某个物品必须满足:, $p_i \leq inc_j \leq s_i$ $|b_i-pref_j| \leq (inc_j-p_i)$…
Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this integer is between 1 and 100 000, inclusive. It is possible that some cards have the same integers on them. Vasily decided to sort the cards. To do this,…
Codeforces 题面传送门 & 洛谷题面传送门 神仙题,做了我整整 2.5h,写篇题解纪念下逝去的中午 后排膜拜 1 年前就独立切掉此题的 ymx,我在 2021 年的第 5270 个小时 A 掉此题,而 ymx 在 2020 年的第 5270 就已经 A 掉了此题 %%%%%% 首先注意到一件事情,就是如果存在一个长度为 \(k\) 的 Journey,那么必然存在一个长度为 \(k\) 的 Journey,满足相邻两个字符串长度的差刚好为 \(1\)(方便起见,在后文中我们及其为 Co…
Codeforces 题目传送门 & 洛谷题目传送门 我怕不是个 nt--一开始忽略了"询问独立"这个条件--然后就一直在想有什么办法维护全局 LIS--心态爆炸 首先离散化.预处理出以每个点为结尾的 LIS 长度 \(f_i\),以及以每个点为开头的 LIS 长度 \(g_i\). 不难发现每次只修改一个元素,故每次询问的答案只可能是原序列 LIS 的长度 \(mx\pm 1\). 我们不妨来探究什么情况下询问的答案为 \(mx+1\),什么情况下询问的答案为 \(mx-1\…
链接 [https://codeforces.com/contest/1092/problem/C] 题意 给你某个字符串的长度n,再给你2*n-2个前缀或者后缀 让你判断那些是前缀那些是后缀 关键是1到n-1的都有两个 分析 只需分析长度为n-1的那两个谁是前缀,谁是后缀 代码 #include<bits/stdc++.h> using namespace std; string s[210]; bool vis[110]; int main(){ int n; //ios::sync_wi…
Dish Shopping 将每个物品拆成p 和 s 再加上人排序. 然后问题就变成了, 对于一个线段(L - R), 问有多少个(li, ri)满足  L >= li && R >= ri, 这个东西可以直接树状数组套平衡树维护. 但是这个题目有个特殊性,因为排好序之后不会存在 li > L && ri > R的点, 所以可以直接 用平衡树, 或者线段树去维护这个东西. 平板电视 #include<bits/stdc++.h> #inc…
题意: 求树上最长上升路径 解析: 树状数组版: 998ms edge[u][w] 代表以u为一条路的终点的小于w的最长路径的路的条数 · 那么edge[v][w] = max(edge[u][w-1]) + 1; 因为w最小是0  所以所有的w都+1 #include <bits/stdc++.h> using namespace std; , INF = 0x7fffffff; int n, m, maxx = -INF; map<int, int> edge[maxn]; i…
B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this integer is between 1 and 10…
题意: 给出一个序列\(A\),有若干询问. 每次询问某个区间中值相等且距离最短的两个数,输出该距离,没有则输出-1. 分析: 令\(pre_i = max\{j| A_j = A_i, j < i\}\),也就是前一个与\(A_i\)相等的数字的下标. 这个可以通过对序列排序,数值相等按照下标排序来计算. 将询问离线,按照询问区间的右端点从左到右排序. 从左到右扫描,向树状数组中在位置\(pre_i\)插入\(i - pre_i\),并维护一个后缀最小值. 查询的时候直接查即可. #inclu…
题目链接: G. Hiring time limit per test 4 seconds memory limit per test 512 megabytes input standard input output standard output The head of human resources department decided to hire a new employee. He created a test exercise for candidates which shoul…
题目 参考:http://blog.csdn.net/xiefubao/article/details/24934617 题意:给两个字符串,求一个最短的子串.使得这个子串在两个字符串中出现的次数都等于1.出现的定义为:可以重叠的出现. 解法:后缀数组的应用.从小枚举长度.如果一个长度len合法的话:则一定存在这个样的sa[i]排名.sa[i]与s[i+1]的公共前缀长度大于等于len,且sa[i]与[i-1]的公共前缀长度小于len,同时sa[i+1]与[i+2]的公共前缀长度小于len,同时…