Codeforces Round #337 Alphabet Permutations
1 second
512 megabytes
standard input
You are given a string s of length n, consisting of first k lowercase English letters.
We define a c-repeat of some string q as a string, consisting of c copies of the string q. For example, string "acbacbacbacb" is a 4-repeat of the string "acb".
Let's say that string a contains string b as a subsequence, if string b can be obtained from a by erasing some symbols.
Let p be a string that represents some permutation of the first k lowercase English letters. We define function d(p) as the smallest integer such that a d(p)-repeat of the string p contains string s as a subsequence.
There are m operations of one of two types that can be applied to string s:
- Replace all characters at positions from li to ri by a character ci.
- For the given p, that is a permutation of first k lowercase English letters, find the value of function d(p).
All operations are performed sequentially, in the order they appear in the input. Your task is to determine the values of function d(p) for all operations of the second type.
The first line contains three positive integers n, m and k (1 ≤ n ≤ 200 000, 1 ≤ m ≤ 20000, 1 ≤ k ≤ 10) — the length of the string s, the number of operations and the size of the alphabet respectively. The second line contains the string s itself.
Each of the following lines m contains a description of some operation:
- Operation of the first type starts with 1 followed by a triple li, ri and ci, that denotes replacement of all characters at positions from lito ri by character ci (1 ≤ li ≤ ri ≤ n, ci is one of the first k lowercase English letters).
- Operation of the second type starts with 2 followed by a permutation of the first k lowercase English letters.
For each query of the second type the value of function d(p).
7 4 3
abacaba
1 3 5 b
2 abc
1 4 4 c
2 cba
6
5
After the first operation the string s will be abbbbba.
In the second operation the answer is 6-repeat of abc: ABcaBcaBcaBcaBcAbc.
After the third operation the string s will be abbcbba.
In the fourth operation the answer is 5-repeat of cba: cbAcBacBaCBacBA.
Uppercase letters means the occurrences of symbols from the string s.
思路:容易证明答案等于1+字符串中相邻字符对不可成为置换串(模式串)子串的对数。
本题关键是如何充分利用题目信息降低计算复杂度。由于操作1和操作2有2e4次,显然不能每次遍历文本串。
注意到串是定长的,因此字符对数固定,而字符相邻关系可以用k * k矩阵表示。
考虑问题的反面,只需计数文本串中所有相邻字符对满足可成为模式串子串的对数。
k*k矩阵记录当前串中字符i与字符j相邻的相邻字符对对数。这样记录的目的是为了应对模式串的改变。
用线段树储存字符串子串相邻字符对出现次数的信息,这样同时标记端点字符即可进行子串的合并。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lson (u << 1)
#define rson (u << 1 | 1)
using namespace std;
const int maxn = 2e5 + ;
char T[maxn], P[];
int n, m, k;
struct Seg{
int l, r;
char left, right;
int lazy;
int mt[][];
}seg[maxn << ]; void push_up(int u){
memset(seg[u].mt, , sizeof seg[u].mt);
for(int i = ; i < k; i++) for(int j = ; j < k; j++) seg[u].mt[i][j] += seg[lson].mt[i][j] + seg[rson].mt[i][j];
seg[u].left = seg[lson].left, seg[u].right = seg[rson].right;
seg[u].mt[seg[lson].right - 'a'][seg[rson].left - 'a']++;
} void build(int u, int l, int r){
seg[u].l = l, seg[u].r = r;
seg[u].left = T[l], seg[u].right = T[r - ];
seg[u].lazy = ;
memset(seg[u].mt, , sizeof seg[u].mt);
if(r - l < ) return;
int mid = (l + r) >> ;
build(lson, l, mid), build(rson, mid, r);
push_up(u);
} void push_down(int u){
if(!seg[u].lazy) return;
memset(seg[lson].mt, , sizeof seg[lson].mt);
memset(seg[rson].mt, , sizeof seg[rson].mt);
seg[lson].mt[seg[u].left - 'a'][seg[u].left - 'a'] = seg[lson].r - seg[lson].l - ;
seg[rson].mt[seg[u].left - 'a'][seg[u].left - 'a'] = seg[rson].r - seg[rson].l - ;
seg[u].lazy = ;
seg[lson].lazy = seg[rson].lazy = ;
seg[lson].left = seg[rson].left = seg[lson].right = seg[rson].right = seg[u].left;
} void cover(int u, int l, int r, int L, int R, char ch){
if(l == L && r == R){
memset(seg[u].mt, , sizeof seg[u].mt);
seg[u].left = seg[u].right = ch;
seg[u].lazy = ;
seg[u].mt[ch - 'a'][ch - 'a'] = r - l - ;
return;
}
push_down(u);
int mid = (l + r) >> ;
if(R <= mid) cover(lson, l, mid, L, R, ch);
else if(L >= mid) cover(rson, mid, r, L, R, ch);
else{
cover(lson, l, mid, L, mid, ch);
cover(rson, mid, r, mid, R, ch);
}
push_up(u);
} int getAns(){
int ans = ;
for(int i = ; i < k; i++) for(int j = i + ; j < k; j++){
ans += seg[].mt[P[i] - 'a'][P[j] - 'a'];
}
ans = n - ans;
return ans;
} int main(){
//freopen("in.txt", "r", stdin);
while(~scanf("%d%d%d", &n, &m, &k)){
scanf("%s", T + );
build(, , n + );
char ch;
for(int i = , op, l, r; i < m; i++){
scanf("%d", &op);
if(op == ){
scanf("%d%d %c", &l, &r, &ch);
cover(, , n + , l, r + , ch);
}else{
scanf("%s", P);
int ans = getAns();
printf("%d\n", ans);
}
}
}
return ;
}
Codeforces Round #337 Alphabet Permutations的更多相关文章
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线
D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...
- Codeforces Round #337 (Div. 2) C. Harmony Analysis 构造
C. Harmony Analysis 题目连接: http://www.codeforces.com/contest/610/problem/C Description The semester i ...
- Codeforces Round #337 (Div. 2) B. Vika and Squares 贪心
B. Vika and Squares 题目连接: http://www.codeforces.com/contest/610/problem/B Description Vika has n jar ...
- Codeforces Round #337 (Div. 2) A. Pasha and Stick 数学
A. Pasha and Stick 题目连接: http://www.codeforces.com/contest/610/problem/A Description Pasha has a woo ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)
题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...
- Codeforces Round #337 (Div. 2) C. Harmony Analysis
题目链接:http://codeforces.com/contest/610/problem/C 解题思路: 将后一个矩阵拆分为四个前一状态矩阵,其中三个与前一状态相同,剩下一个直接取反就行.还有很多 ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并
D. Vika and Segments Vika has an infinite sheet of squared paper. Initially all squares are whit ...
- Codeforces Round #337 (Div. 2)
水 A - Pasha and Stick #include <bits/stdc++.h> using namespace std; typedef long long ll; cons ...
- Codeforces Round #337 Vika and Segments
D. Vika and Segments time limit per test: 2 seconds memory limit per test: 256 megabytes input ...
随机推荐
- 减去border边框
width: 100%; border: 1px solid transparent; box-sizing: border-box;
- 给ubuntu系统换新装
此次安装主要按照Flatabulous:Ubuntu系统美化主题 但自己在安装过程中发现了很多问题,一下一段黑色的是上面链接的文章,红色的是自己在操作过程中的一些改动 安装主题的第一步是安装Ubunt ...
- oracle组建:ODAC112021Xcopy_x64,在开发机上,不用安装oracle的客户端等开发
以下解决方案是为了连接远程服务器上的oracle 11g 的解决方案. 下载地址:http://www.oracle.com/technetwork/database/windows/download ...
- wampserver环境下,安装ucenter1.6.0
1,)从官网下载UCenter_1.6.0_SC_UTF8.zip,解压拷贝upload到www下,重命名upload->ucenter; 2,)D:\wamp\bin\apache\Apach ...
- 当As3遇见Swift(二)
字符串:String 都是用String来表示,都是值类型,在传递过程中都会进行拷贝. 计算字符数量 As3: str.length Swift: countElements(str) 数组:Arra ...
- ofbiz进击 第二节。 control 理解与创建
首先要说的是,学习ofbiz,要去http://ofbiz.apache.org/官网里面,去看右边菜单里 Management Apps 的例子,然后找到类似的页面,去看调用的源码方法. co ...
- ThinkPHP 中实现 Rewrite 模式
ThinkPHP中默认的URL地址是形如这样的:http://localhost/Myapp/index.php/Index/index/ Myapp是我的项目文件名,默认的访问地址是上面这样的.为了 ...
- Sqlserver列出所有数据库名,表名,字段名
Sqlserver列出所有数据库名,表名,字段名 1.获取所有数据库名: ? 1 SELECT Name FROM Master..SysDatabases ORDER BY Name 注 ...
- 关于C# winform 快速制作不规则边框的方法
今天逛博客园突然发现一个帖子写的 快速建立不规则边框的方式 突然发现以前自己用API的方式好傻… 杀鸡焉用牛刀 下边是从网上不断转载的 原帖: 地址:http://www.cnblogs.com ...
- webpack我遇到的一些坑
我的第一个用于实验webpack的项目是一个拥有多个版本的项目.什么叫多个版本?这个项目对3个语言版本+3个不同城市版本+(移动端 + PC端),也就是3*3*2,18个版本. 我的第一次想法肯定是 ...