
字符串
不可知论大祭司
Know how to solve every problem that has been solved.
展开
-
LuoguP3796 【模板】AC自动机(加强版)
link链接code#include <iostream>#include <queue>#include <cstdio>#include <cstring>#include <algorithm>const int N = 1e6 + 10;using namespace std;struct Ans{ int num, pos;}ans[N];bool cmp(Ans a, Ans b){ if原创 2021-03-02 07:58:16 · 94 阅读 · 0 评论 -
LuoguP2580字典树
link链接codetag[num] == 1表示从根节点到编号为num的节点所表示的字符串曾经出现过。为了判断是否重复,可以对已经重复的tag值修改为2.#include <cstdio>#include <iostream>#include <algorithm>using namespace std;const int N = 500010;int m, n, tr[N][26], tag[N];int cnt = 1;int main(原创 2021-03-01 19:28:41 · 107 阅读 · 0 评论 -
P3375 【模板】KMP字符串匹配
code#include <cstdio>#include <algorithm>#include <cstring>#include <cctype>#include <vector>#include <iostream>#include <map>#include <set>using namespace std;inline int read(){ int x = 0, op原创 2021-01-25 13:03:25 · 119 阅读 · 0 评论 -
leetcode 830
class Solution { public List<List<Integer>> largeGroupPositions(String s) { s += "#"; List<List<Integer>> res = new ArrayList<>(); int begin = 0; for (int i = 1; i < s.length(); i++) {原创 2021-01-05 08:54:10 · 110 阅读 · 0 评论 -
中心扩散法解决回文串问题
回文串概述一般的方法概述找一个序列的最长回文串方法有很多,暴力求解,dp,中心扩散,还有Manacher(马拉车)等,中心扩散的时间复杂度O(n^2),而且相比于dp,不需要开辟额外的空间。又比较容易理解。一般的方法如果是判断是否为回文串,我们一般这样做public boolean valid(char[] str, int l, int r){ while (l < r){ if (str[l++] != str[r--]){原创 2020-12-30 12:48:27 · 423 阅读 · 2 评论 -
【CF1428C】ABBB 字符串
链接传送门分析只有AB或BB能够删去,也就是说只要B前面还有字母,那么我们就可以删去,ans指的是已经遍历过的字符剩下的个数,这样做时间复杂度是O(n),满足题目所给的数据范围代码#include <cstdio>#include <cstring>using namespace std;const int N = 2e5 + 10;int t;char s[N];int main() { scanf("%d", &t); while原创 2020-12-27 11:57:03 · 290 阅读 · 0 评论 -
【CF1451B】Non-Substring Subsequence
链接传送门分析要保持子串的有序性,那我们可以想象向子串左右边找,判断是否找到相同的字符就可以了。数据很小,直接遍历。代码#include <iostream>#include <string>#include <cstdio>int t, n, q, l, r;using namespace std;int main() { cin >> t; while (t--){ cin >> n &g原创 2020-12-27 11:02:19 · 186 阅读 · 0 评论 -
字符串匹配 KMP算法
KMP算法介绍next数组代码介绍用传统的暴力匹配的方法进行字符串匹配的时间复杂度太大,效率往往不高。KMP算法减少了很多不必要的比较操作,通过已经匹配主串的前缀中的最长可匹配后缀和模式串中的最长可匹配前缀使得模式串的快速移动。时间复杂度可达O(n + m) 【n,m分别是主串和模式串的长度】next数组算法的关键是找到主串最长可匹配后缀和模式串中的最长可匹配前缀,由于前面的部分是已经匹配的,我们可以直接在模式串中生成next数组。next[j]数组的下标j是最长可匹配前缀串的长度,比如ABA就是1原创 2020-12-17 11:42:41 · 252 阅读 · 0 评论 -
【CF550A】Two Substrings 子字符串
题目概述You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings “AB” and “BA” (the substrings can go in any order).链接传送门分析要注意两点不能重叠ABA究竟是算AB还是BA对于问题2,如果当作AB不行,我们要把它看作BA看看是否可行代码#include <io原创 2020-12-15 14:02:03 · 267 阅读 · 0 评论 -
字符串匹配(一) BF
简介所谓的BF就是brutal force的缩写,设主串的长度为n,模式串长度为m,在最坏的情况下时间复杂度为O(m * n),下面简单用代码实现一下。代码public class BF { public static int find_index(String pattern, String str){ int i = 0, index = 0; while (i < str.length()) { if (pa原创 2020-12-11 12:57:15 · 224 阅读 · 1 评论 -
【CF1037C】 Equalize
Equalize题目大意链接代码题目大意两个01字符串,长度均为n,对第一个字符串有两个操作,每个操作都对应着cost,求把第一个字符串变为第二个的最小cost链接传送门代码注意到除非两个元素相邻,否则交换操作的花费是比改变操作大#include <iostream>#include <string>using namespace std;int n, ans;int main() { string s1, s2; cin >>原创 2020-12-08 08:03:21 · 165 阅读 · 0 评论