C. Yet Another Broken Keyboard
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Recently, Norge found a string s=s1s2…sns=s1s2…sn consisting of nn lowercase Latin letters. As an exercise to improve his typing speed, he decided to type all substrings of the string ss. Yes, all n(n+1)2n(n+1)2 of them!

A substring of ss is a non-empty string x=s[a…b]=sasa+1…sbx=s[a…b]=sasa+1…sb (1≤a≤b≤n1≤a≤b≤n). For example, "auto" and "ton" are substrings of "automaton".

Shortly after the start of the exercise, Norge realized that his keyboard was broken, namely, he could use only kk Latin letters c1,c2,…,ckc1,c2,…,ck out of 2626.

After that, Norge became interested in how many substrings of the string ss he could still type using his broken keyboard. Help him to find this number.

Input

The first line contains two space-separated integers nn and kk (1≤n≤2⋅1051≤n≤2⋅105, 1≤k≤261≤k≤26) — the length of the string ss and the number of Latin letters still available on the keyboard.

The second line contains the string ss consisting of exactly nn lowercase Latin letters.

The third line contains kk space-separated distinct lowercase Latin letters c1,c2,…,ckc1,c2,…,ck — the letters still available on the keyboard.

Output

Print a single number — the number of substrings of ss that can be typed using only available letters c1,c2,…,ckc1,c2,…,ck.

Examples
input

Copy
7 2
abacaba
a b
output

Copy
12
input

Copy
10 3
sadfaasdda
f a d
output

Copy
21
input

Copy
7 1
aaaaaaa
b
output

Copy
0
Note

In the first example Norge can print substrings s[1…2]s[1…2], s[2…3]s[2…3], s[1…3]s[1…3], s[1…1]s[1…1], s[2…2]s[2…2], s[3…3]s[3…3], s[5…6]s[5…6], s[6…7]s[6…7], s[5…7]s[5…7], s[5…5]s[5…5], s[6…6]s[6…6], s[7…7]s[7…7].

还是先学长的
学长原贴: https://www.cnblogs.com/xyq0220/p/12036109.html

学长思路:

在字符串s中所有不能打出的字母的位置作为隔板,隔出来的每个子串(设len为子串的长度)对答案的贡献即为len×(len+1)/2。
 1 #include<bits/stdc++.h>
2 #define fi first
3 #define se second
4 #define lson l,mid,p<<1
5 #define rson mid+1,r,p<<1|1
6 #define pb push_back
7 #define ll long long
8 using namespace std;
9 const int inf=1e9;
10 const int mod=1e9+7;
11 const int maxn=2e5+10;
12 int n,k;
13 char s[maxn],t[30];
14 int main(){
15 ios::sync_with_stdio(false);
16 //freopen("in","r",stdin);
17 cin>>n>>k;
18 cin>>s+1;
19 for(int i=1;i<=k;i++){
20 char c;
21 cin>>c;
22 t[c-'a']=1;
23 }
24 ll ans=0,cnt=0;
25 for(int i=1;i<=n;i++){
26 if(!t[s[i]-'a']){
27 ans+=cnt*(cnt+1)/2;
28 cnt=0;
29 }else ++cnt;
30 }
31 ans+=cnt*(cnt+1)/2;
32 cout<<ans<<endl;
33 return 0;
34 }

我的代码:

 1 #include <bits/stdc++.h>
2 using namespace std;
3 int main(){
4 long long int n,temp=0,k,biao[26],count=0;
5 long long int sum =0;
6 char str[200005],tc;
7 memset(biao,0,sizeof(biao));
8 memset(str,'\0',sizeof(str));
9 cin>>n>>k;
10 temp = n;
11 cin>>str;
12 while(k--){
13 cin>>tc;
14 biao[tc-'a'] = 1;
15 }
16 for(int i = 0;i<n;i++){
17 if(biao[str[i]-'a']){
18 count += 1;
19 }
20 else{
21 sum += count*(count+1)/2;
22 count = 0;
23 }
24 }
25 sum += count*(count+1)/2;
26 cout<<sum<<endl;
27 return 0;
28 }

其实这题又和学长思路一样,把缺的字母当隔板,对每个隔间累加求总值。

可能是这题比较简单(但是比赛我就蒙了???),所以代码差别不大

都用了标记法,这也是这种题的常用手法。

CF1272 C Yet Another Broken Keyboard 题解+代码比对的更多相关文章

  1. uva - Broken Keyboard (a.k.a. Beiju Text)(链表)

    11988 - Broken Keyboard (a.k.a. Beiju Text) You’re typing a long text with a broken keyboard. Well i ...

  2. UVa 11988 Broken Keyboard (a.k.a. Beiju Text)(链表)

    You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem wi ...

  3. PAT1084:Broken Keyboard

    1084. Broken Keyboard (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue On a ...

  4. PAT 1084 Broken Keyboard

    1084 Broken Keyboard (20 分)   On a broken keyboard, some of the keys are worn out. So when you type ...

  5. B - Broken Keyboard (a.k.a. Beiju Text) 数组模拟链表

    You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem wi ...

  6. Broken Keyboard (a.k.a. Beiju Text) 思路

    问题:You’re typing a long text with a broken keyboard. Well it’s not so badly broken. The only problem ...

  7. N - Broken Keyboard (a.k.a. Beiju Text)(DFS,链表)

    N - Broken Keyboard (a.k.a. Beiju Text) Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:% ...

  8. Broken Keyboard(模拟数组或者双重链表的运用)

    这题我是大写的服气,辛辛苦苦搞了个双重链表结果还一直不对,不对就算了,书上源代码打进去还是不对,我能怎么办我也很无奈.不过这题还是让我对双重链表更加了解和运用了!还是可以的! You’re typin ...

  9. UVA——11988 Broken Keyboard (a.k.a. Beiju Text)

    11988 Broken Keyboard (a.k.a. Beiju Text)You’re typing a long text with a broken keyboard. Well it’s ...

  10. 1084. Broken Keyboard (20)【字符串操作】——PAT (Advanced Level) Practise

    题目信息 1084. Broken Keyboard (20) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B On a broken keyboard, some of ...

随机推荐

  1. solt废弃,报错解决方法

    1.饿了么组件库给出得文字提示框 写到项目里之后报错 提示 solt已经废弃 <el-tooltip placement="top"> <div slot=&qu ...

  2. ERA-Interim 的变量TCW和VIWV可降水量

    可降水量(Precipitable water) 气象上有一个名词"可降水量"(Precipitable water),可以用来衡量大气的水含量. 其公式为 \(W=\frac{1 ...

  3. xd p3 搭建安全扩展

    常见搭建平台脚本启用 常见平台java Python php jsp搭建要启用脚本 中间件(搭建平台):Apache IIS Tomcat Nginx 主机头值 即 域名 域名IP目录解析安全问题 域 ...

  4. maven的setting.xml配置文件详解

    <?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://mav ...

  5. C++ primer笔记 -基本语言

    C++最重要的特征是类,程序员可以使用类自定义数据类型,C++有时候将这些类型称为"类类型",以区别于内置类型. 类型作用: 1.告诉我们数据代表的是什么意思 2.对数据可以执行哪 ...

  6. 权昌TSC条码打印机终极使用教程与开发版本代码大全

    本教程使用的打印机型号:TSC TTP-244 Plus 官方文档 一.TSC打印机安装 1.机器安装 根据官方快速安装指南安装打印机,此处不详细说明,也可以看视频教程,唯一需要注意的地方就是碳带的方 ...

  7. 【20】python之操作MySQL数据库

    一.连接库安装 Python2.x:MySQLdb Python3.x :pymysql 二.接口信息 #创建数据库连接 pymysql.Connect()参数说明 host(str): MySQL服 ...

  8. Qt事件处理的几种方式

    Qt提供了5种事件处理和事件过滤的方法: 1.重写事件处理器函数 这是大部分情况最常用的一种,如重写 paintEvent().mousePressEvent().keyPressEvent() 等事 ...

  9. mac系统yarn使用报错:ERROR: add is not COMMAND nor fully qualified CLASSNAME.

    出现错误的过程: mac 系统上使用阿里的X6(@antv/x6) x6 快速上手: 1   npm install @antv/x6 --save 2   yarn add @antv/x6     ...

  10. laravel 邮件发送

    1.首先你要在qq悠闲中开启你的   SMPT(设置->账户)   获取到你的授权码 2.配置laravel MAIL_DRIVER=smtpMAIL_HOST=smtp.qq.comMAIL_ ...