Codeforces 1272C Yet Another Broken Keyboard

思路:

公式都给你了…用坏键把连续的好键隔开,单独算加起来就行了,但是计算过程中会超出int,所以要开long long

代码:

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long LL;
#define fi first
#define sc second
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pt(a) cerr<<a<<"---\n"
#define rp(i,n) for(int i=0;i<n;i++)
#define rpn(i,n) for(int i=1;i<=n;i++)
int flag[30];
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n,k; cin>>n>>k;
	string s; cin>>s;
	rp(i,k){
		string ss; cin>>ss;	
		flag[ss[0]-'a']=1;
	}
	LL x=0;
	LL res=0;
	for(char c:s){
		if(flag[c-'a']) x++;
		else{
			res+=x*(x+1)/2;
			x=0;
		}
	}
	res+=x*(x+1)/2;
	cout<<res;
	return 0;
}
posted @ 2019-12-13 16:10  YuhanのBlog  阅读(107)  评论(0编辑  收藏  举报