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. IntelliJ IDEA 常用设置讲解3

    IntelliJ IDEA 有很多人性化的设置我们必须单独拿出来讲解,也因为这些人性化的设置让我们这些 IntelliJ IDEA 死忠粉更加死心塌地使用它和分享它. 常用设置 如上图 Gif 所示, ...

  2. 2的m次方 内存对齐

    在存储的时候,为了提高效率,一般都会让偏移量落在2的m次方的位置上,而且常有向上取整和向下取整两种需求.向下取整PALIGN_DOWN(x,align)  (x & (- align)) 这样 ...

  3. 实验十五_安装新的int 9中断例程

    安装一个新的int 9中断例程,功能:在DOS下,按下“A”键后,除非不在松开,    如果松开,就显示满屏幕的“A”:其他的键照常处理. 提示:按下一个键时产生的扫描码称为通码,松开一个键产生的扫描 ...

  4. SQL top查询

    select *from emp;

  5. Spring的2个思想

    IOC:控制反转 反转:获取依赖对象的方式被反转了 控制反转是关于一个对象如何获取他所依赖的对象的引用,反转是指责任的反转. (1)对象实例化问题(Spring完成) 传统的依赖方式:程序员实例化 ( ...

  6. HDU 5002 Tree(动态树LCT)(2014 ACM/ICPC Asia Regional Anshan Online)

    Problem Description You are given a tree with N nodes which are numbered by integers 1..N. Each node ...

  7. 杭电oj 1016 Prime Ring Problem

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. Oracle存储过程总结

    1.存储过程结构 1.1 第一个存储过程 create or replace procedure proc1( para1 varchar2, para2 out varchar2, para3 in ...

  9. 二项分布 多项分布 伽马函数 Beta分布

    http://blog.csdn.net/shuimu12345678/article/details/30773929 0-1分布: 在一次试验中,要么为0要么为1的分布,叫0-1分布. 二项分布: ...

  10. dataTabel转成dataview插入列后排序

    if (!string.IsNullOrEmpty(strQuyu) && !string.IsNullOrEmpty(strZuhao)) { string[] param = { ...