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
inputCopy

7 2
abacaba
a b
outputCopy
12
inputCopy
10 3
sadfaasdda
f a d
outputCopy
21
inputCopy
7 1
aaaaaaa
b
outputCopy
0
Note
In the first example Norge can print substrings s[1…2], s[2…3], s[1…3], s[1…1], s[2…2], s[3…3], s[5…6], s[6…7], s[5…7], s[5…5], s[6…6], s[7…7].

题意:给定一个字符串,和k个好的字符,问在字符串中有多少子串是好的字符组成的。(长度为k的字符串子串有k*(k+1)/2);

解析:我们在字符串中找好的字符抱团在一起的。
例 abbcdefabba;
好的字符为 a b
那么有两个抱团abb和abba 然后利用公式就可以求出

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10000;
typedef long long ll;
int n,m,k;
char ch;
string s;
map<int,int>v;
vector<ll> ans;
int main()
{cin>>n>>k;cin>>s;for(int i=0;i<k;i++){cin>>ch;v[ch-'a']=1;}int cnt=0;for(int i=0;i<n;i++){if(v[s[i]-'a']) cnt++;else if(cnt){ans.push_back(cnt);cnt=0;}}if(cnt) ans.push_back(cnt);ll res=0;for(int i=0;i<ans.size();i++){res+=ans[i]*(ans[i]+1)/2;} cout<<res<<endl;
}

C. Yet Another Broken Keyboard--------思维相关推荐

  1. 例题6-4 破损的键盘(又名:悲剧文本)(Broken Keyboard(a.k.a. Beiju Text), UVa 11988)

    例题6-4 破损的键盘(又名:悲剧文本)(Broken Keyboard(a.k.a. Beiju Text), UVa 11988) 链表的应用 #include<iostream> # ...

  2. PAT甲级1084 Broken Keyboard:[C++题解]字符串处理、双指针算法

    文章目录 题目分析 题目来源 题目分析 来源:acwing ac代码1:双指针算法 读入两个字符串a和b,用两个指针i和j分别遍历a.b.如果a[i] ==b[j],说明键盘没坏,j往后移动一个:如果 ...

  3. PAT Broken Keyboard (20)

    题目描写叙述 On a broken keyboard, some of the keys are worn out. So when you type some sentences, the cha ...

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

    原题及翻译 Broken Keyboard (a.k.a. Beiju Text) 破碎的键盘(a.k.a. Beiju Text) You're typing a long text with a ...

  5. 【简便解法】1084 Broken Keyboard (20 分)_16行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 On a broken keyboard, some of the keys are worn out. So when you ...

  6. 【刘汝佳代码详解】例题6-4破损的键盘(Broken Keyboard,UVa 11988)

    立志用最少的代码做最高效的表达 You're typing a long text with a broken keyboard. Well it's not so badly broken. The ...

  7. 1084. Broken Keyboard (20)

    题目例如以下: On a broken keyboard, some of the keys are worn out. So when you type some sentences, the ch ...

  8. Yet Another Broken Keyboard

    Recently, Norge found a string s=s1s2-sns=s1s2-sn consisting of nn lowercase Latin letters. As an ex ...

  9. 11988 - Broken Keyboard (a.k.a. Beiju Text)

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

  10. UVa11988-破损的键盘 Broken Keyboard

    题目描述 You're typing a long text with a broken keyboard. Well it's not so badly broken. The only probl ...

最新文章

  1. xtrabackup2.4 备份Precona5.6数据库,做增量备份与还原
  2. Linux意外之rpm的删除与恢复
  3. STC15分时内核中函数指针的使用问题
  4. 小猪笔记本电脑装linux时需要以下命令
  5. (LINQ 学习系列)(8)Linq教程实例: 事务处理
  6. python字符宽度_Python计算字符宽度的方法
  7. Python是Python的web框架
  8. mysql check约束无效_mysql数据库 check约束无效
  9. electron webview 页面加载事件顺序
  10. 使用ASP.NET Atlas ItemView控件显示集合中的单个数据
  11. php中10的几次方,C++中10的几次方怎么表示
  12. rabbitmq批量确认结合qos导致的消费者假死和消息丢失问题
  13. 项目管理中风险控制的策略
  14. u3d 使用Renderer来修改材质
  15. django的ORM常用字段和参数
  16. 2019-12-17-整理的遥感影像(也包含一些GIS和地学数据)下载地址
  17. 【R语言】如何进行英文分词统计(以《爱丽丝漫游奇境》词频统计为例)(20年3月22日复习笔记)
  18. Windows如何强制关闭电脑全部代理
  19. 在键盘上输入两个int型数据,比较其大小,并输出其中较小的数
  20. Cadence之ORCAD:导出Bom清单

热门文章

  1. linux 查询挂载信息,linux 查询挂载信息
  2. 2011 各大IT公司笔试面试题目
  3. buuctf 荷兰带宽数据泄露
  4. 笔试编程题——数组中出现次数超过一半的数字
  5. FLStudio 四分音符八分音符 四四拍四二拍
  6. IDEA15 tomcat问题
  7. Deepin20.6直接运行exe文件
  8. HbuilderX下载安装
  9. 数据挖掘实战(2)——糖尿病数据集(回归问题)
  10. Android开发学习总结(一)——搭建最新版本的Android开发环境