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. Java基础之一组有用的类——使用比较器对数组排序(TrySortingWithComparator)

    控制台程序. Arrays类中的sort()静态方法把传送为参数的数组元素按升序方式排序. 对于第一个参数类型是Object[]的sort()方法来说,可以传送任意类型的数组.如果使用sort()方法 ...

  2. MVC1

  3. C++类构造析构调用顺序训练(复习专用)

    //对象做函数参数 //1 研究拷贝构造 //2 研究构造函数,析构函数的调用顺序 //总结 构造和析构的调用顺序 #include "iostream" using namesp ...

  4. HDU 4573 Throw the Stones(动态三维凸包)(2013 ACM-ICPC长沙赛区全国邀请赛)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4573 Problem Description Remember our childhood? A fe ...

  5. JSon_零基础_002_将List类型数组转换为JSon格式的对象字符串,返回给界面

    将List类型数组转换为JSon格式的对象字符串,返回给界面 所需要导入的包: 编写bean: package com.west.webcourse.po; /** * 第01步:编写bean类, * ...

  6. linux tomcat 配置

    http://www.cnblogs.com/xdp-gacl/p/4097608.html

  7. Android 沉浸式状态栏

    1,传统的手机状态栏是呈现出黑色或者白色条状的,有的和手机主界面有很明显的区别.这样就在一定程度上牺牲了视觉宽度,界面面积变小.看一下QQ的应用 2,实现起来也挺简单的,来一起看一下吧 MainAct ...

  8. paper 56 :机器学习中的算法:决策树模型组合之随机森林(Random Forest)

    周五的组会如约而至,讨论了一个比较感兴趣的话题,就是使用SVM和随机森林来训练图像,这样的目的就是 在图像特征之间建立内在的联系,这个model的训练,着实需要好好的研究一下,下面是我们需要准备的入门 ...

  9. MVC Controller弹窗的几种方式

    MVC3 Controller弹窗的几种方式   return Content("<script language='javascript' type='text/javascript ...

  10. PAT乙级 1017. A除以B (20)

    1017. A除以B (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求计算A/B,其中A是不超过 ...