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

    CGSizeMake(300, MAXFLOAT),是计算宽和高的,里面的MAXFLOAT通俗点说就是最大的数值,代表你的label的宽和高是随着你label内容而变化,不用担心因为label内容过长 ...

  2. How to debug PostgreSQL function with pgAdminIII

    How to debug plpgsql with pgAdminIII [root@localhost soft_bak]# git clone git://git.postgresql.org/g ...

  3. 【Origin】工仕途中

    -脚步翩跹,随蝶起舞,翩翩不知所往 晨起脚步催, 蝴蝶迎面飞; 正是春意浓, 三月好风景. -作于二零一六年三月二十八日

  4. Python学习总结8:文件模式及操作方法汇总

    文件操作之前需要文件保证文件存在,并且将文件open os.mknod("test.txt")        创建空文件 fp = open("test.txt" ...

  5. Java基础(7):二维数组初始化时需要注意的问题

    二维数组可以先指定行,再指定列:但不能先指定列,再指定行 没有说明二维数组的行的个数,在定义二维数组时也可以只指定行的个数,然后再为每一行分别指定列的个数.如果每行的列数不同,则创建的是不规则的二维数 ...

  6. android 单例模式

    单例模式特点: 1.一个类只能有一个实例 2.自己创建这个实例 3.整个系统都要使用这个实例 单例模式的形式: 1.饿汉式单例类 public class Singleton { private Si ...

  7. 弱类型变量原理探究(转载 http://www.csdn.net/article/2014-09-15/2821685-exploring-of-the-php)

    N首页> 云计算 [问底]王帅:深入PHP内核(一)——弱类型变量原理探究 发表于2014-09-19 09:00| 13055次阅读| 来源CSDN| 36 条评论| 作者王帅 问底PHP王帅 ...

  8. paper 5:支持向量机系列二: Support Vector —— 介绍支持向量机目标函数的 dual 优化推导,并得出“支持向量”的概念。

    paper 4中介绍了支持向量机,结果说到 Maximum Margin Classifier ,到最后都没有说“支持向量”到底是什么东西.不妨回忆一下上次最后一张图: 可以看到两个支撑着中间的 ga ...

  9. Ubuntu + CentOS7 搭建tftp Server

    基于Ubuntu系统做的tftp服务器,基于CentOS 7都差不多,书写了关键命令,测试过Ubuntu 12.0.4 和CentOS 7环境 1.介绍tftp服务器     TFTP(Trivial ...

  10. SqlServer 在创建数据库时候指定的初始数据库大小是不能被收缩的

    当你在SqlServer创建数据库的时候可以指定数据库文件的初始大小,比如下图中我们将新创建的数据库MyDB的大小设置成了1024MB 那么你建好的数据库的确也就会占用1024MB左右的磁盘空间 不过 ...