E. Alphabet Permutations
time limit per test: 

1 second

memory limit per test: 

512 megabytes

input: 

standard input

output: standard output

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:

  1. Replace all characters at positions from li to ri by a character ci.
  2. 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.

Input

The first line contains three positive integers nm 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:

  1. Operation of the first type starts with 1 followed by a triple liri and ci, that denotes replacement of all characters at positions from lito ri by character ci (1 ≤ li ≤ ri ≤ nci is one of the first k lowercase English letters).
  2. Operation of the second type starts with 2 followed by a permutation of the first k lowercase English letters.
Output

For each query of the second type the value of function d(p).

Sample test(s)
input
7 4 3
abacaba
1 3 5 b
2 abc
1 4 4 c
2 cba
output
6
5
Note

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的更多相关文章

  1. 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 ...

  2. Codeforces Round #337 (Div. 2) C. Harmony Analysis 构造

    C. Harmony Analysis 题目连接: http://www.codeforces.com/contest/610/problem/C Description The semester i ...

  3. 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 ...

  4. 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 ...

  5. Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)

    题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...

  6. Codeforces Round #337 (Div. 2) C. Harmony Analysis

    题目链接:http://codeforces.com/contest/610/problem/C 解题思路: 将后一个矩阵拆分为四个前一状态矩阵,其中三个与前一状态相同,剩下一个直接取反就行.还有很多 ...

  7. 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 ...

  8. Codeforces Round #337 (Div. 2)

    水 A - Pasha and Stick #include <bits/stdc++.h> using namespace std; typedef long long ll; cons ...

  9. Codeforces Round #337 Vika and Segments

    D. Vika and Segments time limit per test:  2 seconds     memory limit per test:  256 megabytes input ...

随机推荐

  1. mysql:sql行列转换

    今天一个同学遇到一个问题问我了,由于本人平时学习的mysql比较基础,确实没解决,后来google了一下,才知道是sql的一种技法[行列转换],话不多说先上图: 想得到下面的结果: +------+- ...

  2. j2ee Servlet、Filter、Listener

    首先,JSP/Servlet规范中定义了Servlet.Filter.Listener这三种角色,并没有定义Interceptor这个角色,Interceptor是某些MVC框架中的角色,比如Stru ...

  3. [原创]java WEB学习笔记58:Struts2学习之路---Result 详解 type属性,通配符映射

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  4. Java基础(51):Super与this的区别

    1.     子类的构造函数如果要引用super的话,必须把super放在函数的首位. class Base { Base() { System.out.println("Base" ...

  5. Uploadify在MVC中使用方法案例(一个视图多次上传单张图片)

    Controller 中代码和 上一节文章(http://www.cnblogs.com/yechangzhong-826217795/p/3785842.html )一样 视图中代码如下: < ...

  6. 夺命雷公狗—angularjs—12—get参数的接收

    我们在实际的开发中get和post的交互都是离不开的,我们先来研究下get参数是如何接收到的.. 而且在实际开发中利用json来进行传递参数也是比较多的,这里我们就以get来接收参数为列.. 先创建一 ...

  7. (第九周)视频发布及git统计报告

    项目名:食物链教学工具 组名:奋斗吧兄弟 组长:黄兴 组员:李俞寰.杜桥.栾骄阳.王东涵 代码地址:HTTPS: https://git.coding.net/li_yuhuan/FoodChain. ...

  8. 【NOIP模拟赛】正方形大阵

    正方形大阵 [问题描述]   [输入格式]   第一行一个正整数n代表询问次数. 接下来n行每行一个不超过八位的小数k代表一组询问. [输出格式]   输出共n行,代表每次询问的答案:如果有无数个交点 ...

  9. DB2 表空间和缓冲池

    简介 对于刚涉足 DB2 领域的 DBA 或未来的 DBA 而言,新数据库的设计和性能选择可能会很令人困惑.在本文中,我们将讨论 DBA 要做出重要选择的两个方面:表空间和缓冲池.表空间和缓冲池的设计 ...

  10. android 学习随笔二十四(动画:帧动画)

    帧动画,一张张图片不断的切换,形成动画效果 * 在drawable目录下定义xml文件,子节点为animation-list,在这里定义要显示的图片和每张图片的显示时长 * FrameAnimatio ...