time limit per test2 seconds
memory limit per test256 megabytes
input: standard input
output: standard output

Recently, Norge found a string s=s1s2…sn consisting of n lowercase Latin letters. As an exercise to improve his typing speed, he decided to type all substrings of the string s. Yes, all n(n+1)/2 of them!
A substring of s is a non-empty string x=s[a…b]=sasa+1…sb(1≤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 k Latin letters c1,c2,…,ck out of 26.
After that, Norge became interested in how many substrings of the string s he could still type using his broken keyboard. Help him to find this number.

Input
The first line contains two space-separated integers n and k(1≤n≤2⋅105, 1≤k≤26) — the length of the string s and the number of Latin letters still available on the keyboard.
The second line contains the string s consisting of exactly n lowercase Latin letters.
The third line contains k space-separated distinct lowercase Latin letters c1,c2,…,ck— the letters still available on the keyboard.
Output
Print a single number — the number of substrings of s that can be typed using only available letters c1,c2,…,ck.

Examples
Input
7 2
abacaba
a b
Output
12
Input
10 3
sadfaasdda
f a d
Output
21
Input
7 1
aaaaaaa
b
Output
0

题意:
给出一个字符串,然后给出k个字符,只保留字符串里k的字符的部分,然后求有多少个子串。
某div3的c题,也是一道简单题(只要看懂题)。而我一开始没读懂题意,以为是求字符串里非k字符删去之后的字符串有多少个b不同的子串。直接丢了一个模板上去,然后本地跑的时候发现跟答案并不对。于是研究了大概五分钟后才发现,原来是求每一部分的子串和(题目连公式都给出来了!!!)
英文杀我.jpg
所以策略就是找出连续的k字符有多长然后直接丢进公式再丢进sum就好了

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,k;
string s,s1;
int a[105];
ll sum=0,num=0; int main(){
cin>>n>>k;
cin>>s;
while(k--){
cin>>s1;
a[s1[0]-'a']=1;
}
for(int i=0;i<=n;i++){
if(a[s[i]-'a']==1){
num++;
}else{
sum = sum + num*(num+1)/2;
num=0;
}
}
cout<<sum;
return 0;
}

  

————————————————

CSDN链接:https://blog.csdn.net/weixin_43880627/article/details/103622128

Yet Another Broken Keyboard的更多相关文章

  1. UVa 11998 Broken Keyboard (数组模拟链表问题)

    题目链接: 传送门 Broken Keyboard #include<bits/stdc++.h> using namespace std; char str[100010]; int m ...

  2. UVa 11988 Broken Keyboard(链表->数组实现)

    /*数组形式描述链表:链表不一定要用指针. 题目链接:UVa 11988 Broken Keyboard 题目大意: 小明没有开屏幕输入一个字符串,电脑键盘出现了问题会不定时的录入 home end ...

  3. 1084. Broken Keyboard (20)

    On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters ...

  4. B - Broken Keyboard (a.k.a. Beiju Text)

    Problem B Broken Keyboard (a.k.a. Beiju Text) You're typing a long text with a broken keyboard. Well ...

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

  6. PAT1084:Broken Keyboard

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

  7. PAT 1084 Broken Keyboard

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

  8. A1084. Broken Keyboard

    On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters ...

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

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

随机推荐

  1. JavaWeb入门——Tomcat的目录结构

    JavaWeb入门——Tomcat的目录结构 摘要:本文主要了解了Tomcat的目录结构. 目录结构 打开Tomcat的目录,可以看到如下文件和文件夹: bin目录 该目录下存放的是二进制可执行文件. ...

  2. 龙芯(mips64)电脑安装NodeJS

    背景 龙芯是国产的cpu,采用是mips架构,就类似大家熟知的x86.arm. 最近在一台龙芯电脑(系统是中兴新支点,Linux)上调试前端应用(electron),于是就需要安装NodeJS. 但是 ...

  3. 【漏洞预警】Redis 4.x/5.x 远程命令执行高危漏洞

    2019年7月09日,阿里云应急响应中心监测到有安全研究人员披露Redis 4.x/5.x 远程命令执行高危漏洞利用代码工具.针对未授权或弱口令的Redis服务,攻击者通过构造特定请求,成功利用漏洞可 ...

  4. FloatingActionButton动态更换背景色

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/186 FloatingActionButton 动态更换背 ...

  5. 1.1 菜单管理 ——MyRapid WinForm快速开发框架-功能介绍

    菜单表数据库设计 可以根据数据表取得树状目录,其中 版本和作者 可以分别对版本和修改人进行追溯 有兴趣的朋友可以尝试再添加一个收藏夹   也是比较常用的功能   这里我没有做这个功能 然后看下菜单编辑 ...

  6. [20191127]表 full Hash Value的计算.txt

    [20191127]表 full Hash Value的计算.txt --//曾经做过表full Hash Value的计算,当时我是通过建立简单的schema以及表名的形式,使用hashcat破解o ...

  7. 一个MongoDB索引走偏的案例及探究分析

    接业务需求,有一个MongoDB的简单查询,太耗时了,执行了 70S 左右,严重影响用户的体验.. 查询代码主要如下: db.duoduologmodel.find({"Tags.SN&qu ...

  8. 【tf.keras】tensorflow datasets,tfds

    一些最常用的数据集如 MNIST.Fashion MNIST.cifar10/100 在 tf.keras.datasets 中就能找到,但对于其它也常用的数据集如 SVHN.Caltech101,t ...

  9. iconv转换

    /////////////////////////////////////////////////////// #include <iconv.h> #include <stdlib ...

  10. Docker和ASP.NET Core

    Docker和ASP.NET Core Docker 正在逐渐成为容器行业的事实标准,受到 Windows 和 Linux 生态系统领域最重要供应商的支持. (Microsoft 是支持 Docker ...