可以在这里提交: 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. ThreadPoolExecutor 的三种提交任务方式

    学习内容: ExecutorService线程池的应用... 1.如何创建线程池... 2.调用线程池的方法,获取线程执行完毕后的结果... 3.关闭线程...   首先我们先了解一下到底什么是线程池 ...

  2. MVC Movie App

    ylbtech- ASP.NET MVC:MVC Movie App 功能描述:MVC Movie App 2,TechnologyAndTheEnvironment(技术与环境) 操作系统: win ...

  3. 【Todo】Apache-Commons-Pool及对象池学习

    有这篇文章: http://www.cnblogs.com/tommyli/p/3510095.html 方案提供了三种类型的pool,分别是GenericKeyedObjectPool,SoftRe ...

  4. 查看MySQL数据库大小

    查看MySQL数据库大小 1.首先进入information_schema 数据库(存放了其他的数据库的信息) ? 1 2 mysql> use information_schema; Data ...

  5. java面试题目

    1.Java中的异常处理机制的简单原理和应用.当JAVA程序违反了JAVA的语义规则时,JAVA虚拟机就会将发生的错误表示为一个异常.违反语义规则包括2种情况.一种是JAVA类库内置的语义检查.例如数 ...

  6. 【重点突破】—— 当better-scroll 遇见Vue

    前言:在学习黄轶老师的<Vue.js高仿饿了么外卖App>课程中接触到了better-scroll第三方JavaScript组件库,这是黄轶老师自己基于iscroll重写的库.这里结合黄轶 ...

  7. LeakCanary 的使用遇到的弯路

    基本上来源是:  http://www.liaohuqiu.net/cn/posts/leak-canary-read-me/ 1. demon 中自带的android_v7兼容包有问题的,建议自己使 ...

  8. TestNG+ReportNG+Maven优化测试报告

    转载:https://www.cnblogs.com/hardy-test/p/5354733.html 首先在eclipse里面创建一个maven项目,具体要配置maven环境,请自行百度搭配环境. ...

  9. Ajax库的编写及使用

    ajax使用在服务器端. ajax.js function ajax(url,fnSucc,fnFail) { //1.创建ajax对象 var oAjax = null; if(window.XML ...

  10. android Menory 小结

    不建议在Activity中使用static 变量,考虑使用Application.当然,static final例外 但Application也不要cache某个Activity使用的View,如果c ...