可以在这里提交: http://codeforces.com/gym/100801

题目大意:

给出两个由小写字母组成的字符串S,T,从S中取一个非空前缀,从T中取一个非空后缀,拼接成一个新的字符串。  问这样能得到多少本质不同的新字符串。

|S|,|T|<=1e5

题解:

考虑拼接得到的一个串X,用can[i]=1表示可以用S的前i个字符和T的某个后缀拼接出X。

有一个很重要的性质就是 can[i]=1的段是连续的,也就是说 如果 can[i]=1 can[j]=1 那么 can[k]=1  (i<=k<=j)  在纸上画一下就知道了。

那么如果对于一个串X,统计出有多少个i使得can[i]=can[i+1] 就是重复的次数。

再来看什么情况下can[i]=can[i+1]:

设|S|=n, |T|=m,|X|=l  字符串下标从1开始。

can[i]=can[i+1]  <->   S[i+1]=T[m-(l-i)+1].

所以S中和T中一组相等字符 就对应了 一种 重复的情况。  因此只要统计出S和T中每个字母出现多少次从总数减去就好了。

代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
using namespace std; typedef long long ll;
#define N 100100
map<int,int> mp1,mp2; char s1[N],s2[N];
int main()
{
freopen("concatenation.in","r",stdin);
freopen("concatenation.out","w",stdout); while (scanf("%s%s",s1,s2)!=EOF)
{
mp1.erase(mp1.begin(),mp1.end());
mp2.erase(mp2.begin(),mp2.end());
int n=strlen(s1),m=strlen(s2);
for (int i=;i<n;i++) mp1[s1[i]]++;
for (int i=;i<m-;i++) mp2[s2[i]]++;
ll ans=1ll*n*m;
for (int c='a';c<='z';c++) ans-=1ll*mp1[c]*mp2[c];
printf("%I64d\n",ans);
}
return ;
}

注意在CF上交的时候要用文件输入输出...


加强版:  如果给出多个字符串, 可以任选一个串的前缀和另外一个串的后缀拼接应该怎么做?

只要建一棵prefix trie和suffix trie,答案就是两棵树节点数的乘积 减去sum(cnt1[c]*cnt2[c]) cnt1[c],cnt2[c]分别表示字母c在两棵树中出现的次数。

2015-2016ACM-ICPC NEER northern-subregional-contest C Concatenation的更多相关文章

  1. 【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D】---暑假三校训练

    2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest D Problem D. Distribution in Metagonia Input ...

  2. 2018-2019 ICPC, NEERC, Southern Subregional Contest

    目录 2018-2019 ICPC, NEERC, Southern Subregional Contest (Codeforces 1070) A.Find a Number(BFS) C.Clou ...

  3. Codeforces 2018-2019 ICPC, NEERC, Southern Subregional Contest

    2018-2019 ICPC, NEERC, Southern Subregional Contest 闲谈: 被操哥和男神带飞的一场ACM,第一把做了这么多题,荣幸成为7题队,虽然比赛的时候频频出锅 ...

  4. 模拟赛小结:2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest

    2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest 2019年10月11日 15:35-20:35(Solved 8,Penalty 675 ...

  5. 2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest (9/12)

    $$2015-2016\ ACM-ICPC,\ NEERC,\ Northern\ Subregional\ Contest$$ \(A.Alex\ Origami\ Squares\) 签到 //# ...

  6. ACM ICPC 2016–2017, NEERC, Northern Subregional Contest Problem J. Java2016

    题目来源:http://codeforces.com/group/aUVPeyEnI2/contest/229510 时间限制:2s 空间限制:256MB 题目大意: 给定一个数字c 用 " ...

  7. 2016-2017 ACM-ICPC, NEERC, Northern Subregional Contest Problem F. Format

    题目来源:http://codeforces.com/group/aUVPeyEnI2/contest/229510 时间限制:1s 空间限制:512MB 题目大意: 给定一个字符串,使用%[...] ...

  8. 2016-2017 ACM-ICPC, NEERC, Northern Subregional Contest Problem I. Integral Polygons

    题目来源:http://codeforces.com/group/aUVPeyEnI2/contest/229510 时间限制:2s 空间限制:256MB 题目大意: 给定一个凸多边形,有一种连接两个 ...

  9. 2016-2017 ACM-ICPC, NEERC, Northern Subregional Contest

    A. Anniversary Cake 随便挑两个点切掉就好了. #include<bits/stdc++.h> using namespace std; const int Maxn=2 ...

  10. 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)

    A. Find a Number 找到一个树,可以被d整除,且数字和为s 记忆化搜索 static class S{ int mod,s; String str; public S(int mod, ...

随机推荐

  1. C# 7 新特性-2

    在之前的C# 7 新特性博客中,我们谈到了Tuples,Record Type和Pattern Matching.这些都是C#新特性中最可能出现的.在本博客中,我们会提到更多的一些特性,虽然这些特性不 ...

  2. Kubernetes dashboard集成heapster

    图形化展示度量指标的实现需要集成k8s的另外一个Addons组件: Heapster . Heapster原生支持K8s(v1.0.6及以后版本)和 CoreOS ,并且支持多种存储后端,比如: In ...

  3. Maven 使用 二——nexus

    上篇博客介绍了创建maven项目的两种方式,当中一种是使用命令行的方式来创建,这种情况非常少,一般我们都有IDE开发环境.所以接下来,我们还是在一个详细的IDE中来说,我使用的是Eclipse. 一. ...

  4. numpy基础知识

    官网简介: http://www.numpy.org/ ndarry基本属性 ndarry是Numpy中的N维数组对象(N dimentional arrya,ndarray) ndarry中所有的元 ...

  5. win下写任务提交给集群

    一,复制和删除hdfs中的文件 import org.apache.hadoop.fs.{FileSystem, Path} import org.apache.spark.{SparkConf, S ...

  6. 10 Essential TypeScript Tips And Tricks For Angular Devs

    原文: https://www.sitepoint.com/10-essential-typescript-tips-tricks-angular/ ------------------------- ...

  7. nginx linux 安装

    nginx linux 安装 进入http://nginx.org/en/download.html 下载 n  gcc 安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gc ...

  8. 注册表 API 以及开机自启动

    注册表是window系统中非常重要的一部分,今天在网上查了一些文章学习了下,觉得其中有一句话总结的很经典:注册表是用来存储信息的. 这句话虽然有点废,但是说的没错.当然,注册表中包含的内容非常多,远没 ...

  9. javaSE中的输入输出流---一个读取流,相应多个输出流。并且生成的碎片文件都有有序的编号

    <span style="font-size:18px;">package com.io.other.split; import java.io.File; impor ...

  10. MySQL-配置优化技巧

    一.连接请求配置 1.查询当前连接数(show full processlist) show full processlist; 2.最大连接数(max_connections) max_connec ...