日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Yet Another Broken Keyboard

發(fā)布時(shí)間:2023/12/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Yet Another Broken Keyboard 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Recently, Norge found a string s=s1s2…sns=s1s2…sn consisting of nn lowercase Latin letters. As an exercise to improve his typing speed, he decided to type all substrings of the string ss. Yes, all n(n+1)2n(n+1)2 of them!

A substring of ss is a non-empty string x=s[a…b]=sasa+1…sbx=s[a…b]=sasa+1…sb (1≤a≤b≤n1≤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 kk Latin letters c1,c2,…,ckc1,c2,…,ck out of 2626.

After that, Norge became interested in how many substrings of the string ss he could still type using his broken keyboard. Help him to find this number.

Input
The first line contains two space-separated integers nn and kk (1≤n≤2?1051≤n≤2?105, 1≤k≤261≤k≤26) — the length of the string ss and the number of Latin letters still available on the keyboard.

The second line contains the string ss consisting of exactly nn lowercase Latin letters.

The third line contains kk space-separated distinct lowercase Latin letters c1,c2,…,ckc1,c2,…,ck — the letters still available on the keyboard.

Output
Print a single number — the number of substrings of ss that can be typed using only available letters c1,c2,…,ckc1,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
Note
In the first example Norge can print substrings s[1…2]s[1…2], s[2…3]s[2…3], s[1…3]s[1…3], s[1…1]s[1…1], s[2…2]s[2…2], s[3…3]s[3…3], s[5…6]s[5…6], s[6…7]s[6…7], s[5…7]s[5…7], s[5…5]s[5…5], s[6…6]s[6…6], s[7…7]s[7…7].
比較水的一道題目,主要就是考察一個(gè)字符串的子串有多少個(gè)。
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;vector<int> p; string s; int n,m;inline void dfs(int &i,ll &cnt) {if(i==n) return ;int pos=lower_bound(p.begin(),p.end(),s[i]-'a')-p.begin();if(p[pos]!=s[i]-'a') return ;dfs(i+=1,cnt+=1); } int main() {scanf("%d%d",&n,&m);cin>>s;char c;for(int i=0;i<m;i++) cin>>c,p.push_back(c-'a');//for(int i=0;i<p.size();i++) cout<<p[i]<<" ";cout<<endl;sort(p.begin(),p.end());ll ans=0;ll cnt=0;for(int i=0;i<n;){int pos=lower_bound(p.begin(),p.end(),s[i]-'a')-p.begin();if(p[pos]==(s[i]-'a')){cnt++;i++;dfs(i,cnt);ans+=(cnt*(cnt+1))/2;}else cnt=0,i++;}cout<<ans<<endl;return 0; }

努力加油a啊,(o)/~

總結(jié)

以上是生活随笔為你收集整理的Yet Another Broken Keyboard的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。