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. ES6之Class类

    一.Class的基本语法 1.简介 基本上,ES6的class可以看作只是一个 语法糖,它的绝大部分功能,ES5都可以做到,新的class写法只是让 对象原型 的写法更加清晰.更像面向对象编程的语法而 ...

  2. CLAMP 1.0.1 Vulnhub Walkthrough

    主机层面端口扫描探测: ╰─ nmap -p1-65535 -A -sV 10.10.202.137 访问web服务 使用dirbuster 加大字段进行目录爆破 http://10.10.202.1 ...

  3. java基础学习笔记 第二周(面向对象)

    Day01 什么是抽象数据类型:将不同数据类型的集合组成的一个整体,我们称为抽象数据类型 类就是一个抽象数据类型 成员变量:类中的数据类型就是成员变量(属性) 方法:类中的一些行为就是方法 面向过程( ...

  4. 12-Factor与微服务

           为了构建分布式微服务程序,能够部署到所有云服务,Heroku工程师总结所有云原生应用程序的12要素: 1.基准代码 Single codebase: The application mu ...

  5. katalon Studio之WebUi自动化测试视频教程持续更新

    通知...通知...通知... 为了更好的把katalon Studio自动化测试工具推广给大家,最近在B站中开通了视频专栏,地址如下: https://www.bilibili.com/video/ ...

  6. Node.js Error简介以及捕获方式

    error的类型nodejs 的error 一般分为四种类型: 1.标准的 JavaScript 错误,例如 EvalError.SyntaxError.RangeError.ReferenceErr ...

  7. 菜鸟刷面试题(一、Java基础篇)

    目录: JDK 和 JRE 有什么区别? == 和 equals 的区别是什么? 两个对象的 hashCode()相同,则 equals()也一定为 true,对吗? final 在 java 中有什 ...

  8. Bandizip解压压缩软件

    上次重装系统以后,特别注意安装软件,在这里提一些比较好的 解压软件:Bandizip 这个是一个免费轻量级的软件,压缩解压速度快,没什么广告,推荐大家在官方下载 Bandizip下载网址 点击下载安装 ...

  9. fjnu2019第二次友谊赛 F题

    ### 题目链接 ### 题目大意: 一开始手上有 z 个钱币,有 n 天抉择,m 种投资方案,在每天中可以选择任意种投资方案.任意次地花费 x 个钱币(手上的钱币数不能为负),使得在 n 天结束后, ...

  10. Spring注解的使用和组件扫描

    非常重要] 组件扫描(Component-Scan) 通过配置组件扫描,可以使得spring自动扫描package,而不必在spring的配置文件中逐一声明各个<bean> 在配置组件扫描 ...