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 ...
随机推荐
- ES-索引操作
参考: https://es.xiaoleilu.com/030_Data/05_Document.html <ELasticsearch in Action> 以下的操作在ES7.5版本 ...
- css position:sticky的尝试
前言 sticky这种设计效果是经常出现的,比如陶宝右侧的工具栏,当我们向下滚动到它的位置时,它就会黏住顶部跟随滚动,类似position: fixed的效果,只不过它的触发条件是当我们滚动到所在位置 ...
- 关于for循环中使用setTimeout的四种解决方案
我们先来简单了解一下setTimeout延时器的运行机制.setTimeout会先将回调函数放到等待队列中,等待区域内其他主程序执行完毕后,按时间顺序先进先出执行回调函数.本质上是作用域的问题. 因此 ...
- Linux下安装及使用mysql
(注:本人在centos7进行的安装及使用) 1.安装wget yum install wget 2.下载mysql安装包 wget http://repo.mysql.com/mysql57-com ...
- 为什么 netstat 对某些服务只显示了 tcp6 监听端口
最近偶尔发现一个比较奇怪的现象,netstat 查看监听的服务端口时,却只显示了 tcp6 的监控, 但是服务明明是可以通过 tcp4 的 ipv4 地址访问的,那为什么没有显示 tcp4 的监听呢? ...
- C# SendAysnc 超时
业务方法 @@@code public override async Task<List<(IPEndPoint endPoint, byte[] data)>> Send ...
- C#&.Net干货分享- 构造BaiduLanguageHelper对接百度的语言翻译
全部是封装的API源码... namespace Frame.Api{ /// <summary> /// 百度多语言支持器 /// </summary> ...
- 12.2 中的Data Guard Standby 密码文件自动同步 (Doc ID 2307365.1)
Data Guard Standby Automatic Password file Synchronization in 12.2 (Doc ID 2307365.1) APPLIES TO: Or ...
- 常用的git和repo命令
首先下图是git的流程图 相关概念 svn与git命令的对比 git常用命令 git log // 查看当前库的git log信息 git status ./ // 查看当前库的状态 git diff ...
- 用iText5-1-生成PDF
参考代码和图片出处 https://howtodoinjava.com/library/read-generate-pdf-java-itext/ pom引入jar包 <dependencies ...