码迷,mamicode.com
首页 > 其他好文 > 详细

Yet Another Broken Keyboard

时间:2019-12-21 00:15:46      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:strong   example   after   rest   length   details   研究   简单题   fir   

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

  


————————————————

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

Yet Another Broken Keyboard

标签:strong   example   after   rest   length   details   研究   简单题   fir   

原文地址:https://www.cnblogs.com/jjmmboom/p/12075385.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!