Recently, Norge found a string s=s1s2…sns=s1s2…sn consisting of nn lowercase Latin letters. As an exercise to improve his typing speed, he decided to type all substrings of the string ss. Yes, all n(n+1)2n(n+1)2 of them!

A substring of ss is a non-empty string x=s[a…b]=sasa+1…sbx=s[a…b]=sasa+1…sb (1≤a≤b≤n1≤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 kk Latin letters c1,c2,…,ckc1,c2,…,ck out of 2626.

After that, Norge became interested in how many substrings of the string ss he could still type using his broken keyboard. Help him to find this number.

Input
The first line contains two space-separated integers nn and kk (1≤n≤2⋅1051≤n≤2⋅105, 1≤k≤261≤k≤26) — the length of the string ss and the number of Latin letters still available on the keyboard.

The second line contains the string ss consisting of exactly nn lowercase Latin letters.

The third line contains kk space-separated distinct lowercase Latin letters c1,c2,…,ckc1,c2,…,ck — the letters still available on the keyboard.

Output
Print a single number — the number of substrings of ss that can be typed using only available letters c1,c2,…,ckc1,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
Note
In the first example Norge can print substrings s[1…2]s[1…2], s[2…3]s[2…3], s[1…3]s[1…3], s[1…1]s[1…1], s[2…2]s[2…2], s[3…3]s[3…3], s[5…6]s[5…6], s[6…7]s[6…7], s[5…7]s[5…7], s[5…5]s[5…5], s[6…6]s[6…6], s[7…7]s[7…7].
比较水的一道题目,主要就是考察一个字符串的子串有多少个。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;vector<int> p;
string s;
int n,m;inline void dfs(int &i,ll &cnt)
{if(i==n) return ;int pos=lower_bound(p.begin(),p.end(),s[i]-'a')-p.begin();if(p[pos]!=s[i]-'a') return ;dfs(i+=1,cnt+=1);
}
int main()
{scanf("%d%d",&n,&m);cin>>s;char c;for(int i=0;i<m;i++) cin>>c,p.push_back(c-'a');//for(int i=0;i<p.size();i++) cout<<p[i]<<" ";cout<<endl;sort(p.begin(),p.end());ll ans=0;ll cnt=0;for(int i=0;i<n;){int pos=lower_bound(p.begin(),p.end(),s[i]-'a')-p.begin();if(p[pos]==(s[i]-'a')){cnt++;i++;dfs(i,cnt);ans+=(cnt*(cnt+1))/2;}else cnt=0,i++;}cout<<ans<<endl;return 0;
}

努力加油a啊,(o)/~

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

  9. UVa11988-破损的键盘 Broken Keyboard

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

最新文章

  1. pytorch .item_pytorch + SGD
  2. IOT物联网观察之三大运营商物联网发展进入规模商用阶段
  3. 二极管7种应用电路详解之三
  4. mysql 事物状态有几种_mysql第三章 事务以及日志
  5. rabbitmq 连接报错 An unexpected connection driver error occured(亲测)
  6. spring指导的index.html在spring文件夹中的位置
  7. PHP正则表达式详解(三)
  8. windows自动设置ip地址脚本
  9. Linux线程的终止
  10. WeChat for Linux
  11. LibPcap丢包问题
  12. 分析家数据格式、结构
  13. ue4 项目模板_卡牌游戏项目模板
  14. 浅谈用户全生命周期管理
  15. python提取word中的表格写入excel
  16. 八图片在线图片设置付费
  17. code first修改表或字段
  18. scrapy-selenium-谷歌浏览器爬取带有时效性cookies的网站
  19. C语言程序设计李,C语言程序设计4--李2012.ppt
  20. 18.3.7给小可爱们的(NBUOJ)

热门文章

  1. GAN实现半监督学习
  2. mysqldump导出数据库视图_mysql数据库的基本操作:索引、视图,导入和导出,备份和恢复...
  3. linux .o文件 复制,Linux I/O实现文件复制
  4. java 接口 提供 路径,Java之Resource接口
  5. 用Setup Factory打包Visual C++ 2008开发的程序心得总结
  6. Android中保存数据的三种方法
  7. 用linux命令创建表主键,Linux系统下SQL Server数据库操作
  8. python之美_Python之美[从菜鸟到高手]--生成器之全景分析
  9. atom-design(Vue.js移动端组件库)手势组件介绍
  10. 以HANA为核心 SAP实时数据平台详解