Yet Another Broken Keyboard
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的更多相关文章
- UVa 11998 Broken Keyboard (数组模拟链表问题)
题目链接: 传送门 Broken Keyboard #include<bits/stdc++.h> using namespace std; char str[100010]; int m ...
- UVa 11988 Broken Keyboard(链表->数组实现)
/*数组形式描述链表:链表不一定要用指针. 题目链接:UVa 11988 Broken Keyboard 题目大意: 小明没有开屏幕输入一个字符串,电脑键盘出现了问题会不定时的录入 home end ...
- 1084. Broken Keyboard (20)
On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters ...
- 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 ...
- 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 ...
- PAT1084:Broken Keyboard
1084. Broken Keyboard (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue On a ...
- PAT 1084 Broken Keyboard
1084 Broken Keyboard (20 分) On a broken keyboard, some of the keys are worn out. So when you type ...
- A1084. Broken Keyboard
On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters ...
- 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 ...
- 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 ...
随机推荐
- CLAMP 1.0.1 Vulnhub Walkthrough
主机层面端口扫描探测: ╰─ nmap -p1-65535 -A -sV 10.10.202.137 访问web服务 使用dirbuster 加大字段进行目录爆破 http://10.10.202.1 ...
- Java实现Kafka的生产者和消费者例子
Kafka的结构与RabbitMQ类似,消息生产者向Kafka服务器发送消息,Kafka接收消息后,再投递给消费者.生产者的消费会被发送到Topic中,Topic中保存着各类数据,每一条数据都使用键. ...
- 团队开发的代码管理(VS)
1.文档 代码需要一个文档说明代码的基本情况,使用的组件,代码逻辑层等等 2.源代码冲突(Git) 首先需要尽可能避免冲突,公共的工具基类尽可能不动,如果需要修改也交给专人修改不能谁都上去修改 项目按 ...
- 解决Maven无法下载fastdfs-client-java依赖
异常信息:Missing artifact org.csource:fastdfs-client-java:jar:1.27-SNAPSHOT 解决方案:jar包在Maven的中央仓库中缺失,需要手动 ...
- 数据库 tcp协程实现并发 回调函数
数据库 tcp协程实现并发 回顾 一.回顾 进程池,线程池,回调函数 # from gevent import monkey;monkey.patch_all() #补丁 from gevent im ...
- enable user-defined extended attributes for ext3 file systems; 增加ext3 文件系统的扩展属性;
To enable user-defined extended attributes for ext3 file systems (i.e. device), use: tune2fs -o user ...
- golang+webgl实践激光雷达(一)激光扫描仪基础知识
一.前言 最近做一个测量料堆形状的项目,通过前期调研,最后决定用激光测距原理进行测量.通过旋转云台+激光扫描仪实现空间三维坐标的测量.其中激光扫描仪扫射的是一个二维的扫描面,再通过云台旋转,则形成一个 ...
- python文件的使用
文件是一个存储在辅助存储器上的数据序列,可以包含任何数据内容.概念上,文件是数据的集合抽象,类似地,函数是程序的集合和抽象.用文件形式组织和表达数据更有效也更为灵活.文件包括两种类型:文本文件和二进制 ...
- mysql中的事务隔离级别
事务是逻辑上的一组操作,要么都执行,要么都不执行. 事务最经典的.经常被拿出来说的例子就是转账了.假如小花要给小白转账1000元,这个转账会涉及到两个关键操作就是:将小花的余额-1000,将小白的余额 ...
- C# 英语纠错 LanguageTool
WPF中,对单词拼写错误,textbox有相应的附加属性可以设置. <TextBox SpellCheck.IsEnabled="True" /> 但是此属性只在WPF ...