C. Yet Another Broken Keyboard--------思维
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--------思维相关推荐
- 例题6-4 破损的键盘(又名:悲剧文本)(Broken Keyboard(a.k.a. Beiju Text), UVa 11988)
例题6-4 破损的键盘(又名:悲剧文本)(Broken Keyboard(a.k.a. Beiju Text), UVa 11988) 链表的应用 #include<iostream> # ...
- PAT甲级1084 Broken Keyboard:[C++题解]字符串处理、双指针算法
文章目录 题目分析 题目来源 题目分析 来源:acwing ac代码1:双指针算法 读入两个字符串a和b,用两个指针i和j分别遍历a.b.如果a[i] ==b[j],说明键盘没坏,j往后移动一个:如果 ...
- PAT Broken Keyboard (20)
题目描写叙述 On a broken keyboard, some of the keys are worn out. So when you type some sentences, the cha ...
- 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 ...
- 【简便解法】1084 Broken Keyboard (20 分)_16行代码AC
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 On a broken keyboard, some of the keys are worn out. So when you ...
- 【刘汝佳代码详解】例题6-4破损的键盘(Broken Keyboard,UVa 11988)
立志用最少的代码做最高效的表达 You're typing a long text with a broken keyboard. Well it's not so badly broken. The ...
- 1084. Broken Keyboard (20)
题目例如以下: On a broken keyboard, some of the keys are worn out. So when you type some sentences, the ch ...
- Yet Another Broken Keyboard
Recently, Norge found a string s=s1s2-sns=s1s2-sn consisting of nn lowercase Latin letters. As an ex ...
- 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 ...
- UVa11988-破损的键盘 Broken Keyboard
题目描述 You're typing a long text with a broken keyboard. Well it's not so badly broken. The only probl ...
最新文章
- xtrabackup2.4 备份Precona5.6数据库,做增量备份与还原
- Linux意外之rpm的删除与恢复
- STC15分时内核中函数指针的使用问题
- 小猪笔记本电脑装linux时需要以下命令
- (LINQ 学习系列)(8)Linq教程实例: 事务处理
- python字符宽度_Python计算字符宽度的方法
- Python是Python的web框架
- mysql check约束无效_mysql数据库 check约束无效
- electron webview 页面加载事件顺序
- 使用ASP.NET Atlas ItemView控件显示集合中的单个数据
- php中10的几次方,C++中10的几次方怎么表示
- rabbitmq批量确认结合qos导致的消费者假死和消息丢失问题
- 项目管理中风险控制的策略
- u3d 使用Renderer来修改材质
- django的ORM常用字段和参数
- 2019-12-17-整理的遥感影像(也包含一些GIS和地学数据)下载地址
- 【R语言】如何进行英文分词统计(以《爱丽丝漫游奇境》词频统计为例)(20年3月22日复习笔记)
- Windows如何强制关闭电脑全部代理
- 在键盘上输入两个int型数据,比较其大小,并输出其中较小的数
- Cadence之ORCAD:导出Bom清单