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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值