Yet Another Broken Keyboard

本文介绍了一道编程题目,涉及到一个长度为n的字符串s和一个包含k个可用拉丁字母的键盘。目标是计算字符串s中能用键盘上可用字母输入的子串数量。给出了输入输出示例及题目解析,解释了如何计算连续可用字符的子串总数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值