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


Meaning of the questions:
Given a string, and then gives k characters, only part of the string of characters in k reservations, and then ask how many sub-string.
C div3 of a problem, but also a simple question (just read the title). And I started not understand the meaning of problems, that is seeking string Non-k character string after deleting how many different b substring. Direct throw up a template, then found the time to run with the local answer is not right. So the study about five minutes after the discovery, the original is seeking substring and each part (even the title equation gave out !!!)
English kill me .jpg
So strategy is to find out how long a continuous character k then directly thrown into the equation and then thrown into the sum just fine


Code

#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;
}

  


----------------

CSDN link: https: //blog.csdn.net/weixin_43880627/article/details/103622128

Guess you like

Origin www.cnblogs.com/jjmmboom/p/12075385.html