CF1272 C Yet Another Broken Keyboard 题解+代码比对
2 seconds
256 megabytes
standard input
standard output
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.
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.
Print a single number — the number of substrings of ss that can be typed using only available letters c1,c2,…,ckc1,c2,…,ck.
7 2
abacaba
a b
12
10 3
sadfaasdda
f a d
21
7 1
aaaaaaa
b
0
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].
还是先学长的
学长原贴: https://www.cnblogs.com/xyq0220/p/12036109.html
学长思路:
在字符串s中所有不能打出的字母的位置作为隔板,隔出来的每个子串(设len为子串的长度)对答案的贡献即为len×(len+1)/2。
1 #include<bits/stdc++.h>
2 #define fi first
3 #define se second
4 #define lson l,mid,p<<1
5 #define rson mid+1,r,p<<1|1
6 #define pb push_back
7 #define ll long long
8 using namespace std;
9 const int inf=1e9;
10 const int mod=1e9+7;
11 const int maxn=2e5+10;
12 int n,k;
13 char s[maxn],t[30];
14 int main(){
15 ios::sync_with_stdio(false);
16 //freopen("in","r",stdin);
17 cin>>n>>k;
18 cin>>s+1;
19 for(int i=1;i<=k;i++){
20 char c;
21 cin>>c;
22 t[c-'a']=1;
23 }
24 ll ans=0,cnt=0;
25 for(int i=1;i<=n;i++){
26 if(!t[s[i]-'a']){
27 ans+=cnt*(cnt+1)/2;
28 cnt=0;
29 }else ++cnt;
30 }
31 ans+=cnt*(cnt+1)/2;
32 cout<<ans<<endl;
33 return 0;
34 }
我的代码:
1 #include <bits/stdc++.h>
2 using namespace std;
3 int main(){
4 long long int n,temp=0,k,biao[26],count=0;
5 long long int sum =0;
6 char str[200005],tc;
7 memset(biao,0,sizeof(biao));
8 memset(str,'\0',sizeof(str));
9 cin>>n>>k;
10 temp = n;
11 cin>>str;
12 while(k--){
13 cin>>tc;
14 biao[tc-'a'] = 1;
15 }
16 for(int i = 0;i<n;i++){
17 if(biao[str[i]-'a']){
18 count += 1;
19 }
20 else{
21 sum += count*(count+1)/2;
22 count = 0;
23 }
24 }
25 sum += count*(count+1)/2;
26 cout<<sum<<endl;
27 return 0;
28 }
其实这题又和学长思路一样,把缺的字母当隔板,对每个隔间累加求总值。
可能是这题比较简单(但是比赛我就蒙了???),所以代码差别不大
都用了标记法,这也是这种题的常用手法。
CF1272 C Yet Another Broken Keyboard 题解+代码比对的更多相关文章
- uva - Broken Keyboard (a.k.a. Beiju Text)(链表)
11988 - Broken Keyboard (a.k.a. Beiju Text) You’re typing a long text with a broken keyboard. Well i ...
- UVa 11988 Broken Keyboard (a.k.a. Beiju Text)(链表)
You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem wi ...
- PAT1084:Broken Keyboard
1084. Broken Keyboard (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue On a ...
- PAT 1084 Broken Keyboard
1084 Broken Keyboard (20 分) On a broken keyboard, some of the keys are worn out. So when you type ...
- B - Broken Keyboard (a.k.a. Beiju Text) 数组模拟链表
You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem wi ...
- Broken Keyboard (a.k.a. Beiju Text) 思路
问题:You’re typing a long text with a broken keyboard. Well it’s not so badly broken. The only problem ...
- N - Broken Keyboard (a.k.a. Beiju Text)(DFS,链表)
N - Broken Keyboard (a.k.a. Beiju Text) Time Limit:1000MS Memory Limit:0KB 64bit IO Format:% ...
- Broken Keyboard(模拟数组或者双重链表的运用)
这题我是大写的服气,辛辛苦苦搞了个双重链表结果还一直不对,不对就算了,书上源代码打进去还是不对,我能怎么办我也很无奈.不过这题还是让我对双重链表更加了解和运用了!还是可以的! You’re typin ...
- UVA——11988 Broken Keyboard (a.k.a. Beiju Text)
11988 Broken Keyboard (a.k.a. Beiju Text)You’re typing a long text with a broken keyboard. Well it’s ...
- 1084. Broken Keyboard (20)【字符串操作】——PAT (Advanced Level) Practise
题目信息 1084. Broken Keyboard (20) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B On a broken keyboard, some of ...
随机推荐
- 自考网络原理:安全套接字层SSL
对ssl/tls的理解 前:SSL; 后:TLS: 以下是B站上的up主讲的,非常的深入浅出,讲的很好.感谢技术蛋老师. https://www.bilibili.com/video/BV1KY411 ...
- Mogdb / opengauss 用户密码错误,用户被锁
# 问题概述xxx客户新上一套opengauss数据库,在测试中程序里用户的密码配置错误,导致用户被锁# 解决方案1.跟oracle 语法一样alter user xxx account unlock ...
- .net基础—委托和事件
委托 委托是一种引用类型,表示对具有特定参数列表和返回类型的方法的引用. 在实例化委托时,可以将其实例与任何具有兼容签名和返回类型的方法相关联. 可以通过委托实例调用方法.可以将任何可访问类或结构中与 ...
- ubuntu的vsftpd
先输入vsftp -v查看自己的ubuntu是不是已经安装了vsftpd 没有安装的样子 安装的样子 如果没有安装先安装,输入下面的东西 apt-get install vsftpd 进行安装,安装好 ...
- Flink 同时按照计数和时间触发窗口计算
自定义trigger 继承 抽象类 Trigger<T, TimeWindow> 主要实现 Trigger类的四个方法(onElement.onEventTime.onProcessin ...
- (已解决)问题:windows下,为THINKPHP配置apache虚拟主机。
1. 环境和需求 win10_x64,php7,apache24,thinkphp5. 现在,我要用apache给THINKPHP的一个项目设置一个虚拟主机,需要配置. 2. 步骤 a. 首先,找到h ...
- Dapper上传图片
<h2>添加商品</h2> <table class="table table-bordered"> <tr> <td> ...
- Python基础数据类型-Number(数字)
a = -1 # int b = 2.0 # float c = 13.11 # float d = 3.14j # complex print(type(a), type(b), type(c), ...
- vue-cli4初探、问题解决和心得
创建.调试.打包 vue create event-vue npm run serve //这个好,官方推荐`vue serve`没用 npm run build 项目结构 package.json ...
- 二叉树系列之Treap树
Treap是一棵拥有键值.优先级两种权值的树 struct node{ int size;//以这个结点为根的子树的结点总数量,用于名次树 int rank;//优先级 int key ...