E. String Multiplication
2 seconds
256 megabytes
standard input
standard output
Roman and Denis are on the trip to the programming competition. Since the trip was long, they soon got bored, and hence decided to came up with something. Roman invented a pizza's recipe, while Denis invented a string multiplication. According to Denis, the result of multiplication (product) of strings ss of length mm and tt is a string t+s1+t+s2+…+t+sm+tt+s1+t+s2+…+t+sm+t, where sisi denotes the ii-th symbol of the string ss, and "+" denotes string concatenation. For example, the product of strings "abc" and "de" is a string "deadebdecde", while the product of the strings "ab" and "z" is a string "zazbz". Note, that unlike the numbers multiplication, the product of strings ss and ttis not necessarily equal to product of tt and ss.
Roman was jealous of Denis, since he invented such a cool operation, and hence decided to invent something string-related too. Since Roman is beauty-lover, he decided to define the beauty of the string as the length of the longest substring, consisting of only one letter. For example, the beauty of the string "xayyaaabca" is equal to 33, since there is a substring "aaa", while the beauty of the string "qwerqwer" is equal to 11, since all neighboring symbols in it are different.
In order to entertain Roman, Denis wrote down nn strings p1,p2,p3,…,pnp1,p2,p3,…,pn on the paper and asked him to calculate the beauty of the string (…(((p1⋅p2)⋅p3)⋅…)⋅pn(…(((p1⋅p2)⋅p3)⋅…)⋅pn, where s⋅ts⋅t denotes a multiplication of strings ss and tt. Roman hasn't fully realized how Denis's multiplication works, so he asked you for a help. Denis knows, that Roman is very impressionable, he guarantees, that the beauty of the resulting string is at most 109109.
The first line contains a single integer nn (2≤n≤1000002≤n≤100000) — the number of strings, wroted by Denis.
Next nn lines contain non-empty strings p1,p2,…,pnp1,p2,…,pn, consisting of lowercase english letters.
It's guaranteed, that the total length of the strings pipi is at most 100000100000, and that's the beauty of the resulting product is at most 109109.
Print exactly one integer — the beauty of the product of the strings.
3
a
b
a
3
2
bnn
a
1
In the first example, the product of strings is equal to "abaaaba".
In the second example, the product of strings is equal to "abanana".
考虑每一个字符,分情况模拟即可:
对于a+ba+b,
如果bb是一个全为xx的串,仅会连接原来的字符xx,其余置1;
如果bb是一个首末连续段不相接但是都为xx的段,修改xx,其余置1;
如果bb是一个首末连续段不相接为x,y(x!=y)x,y(x!=y)的段,修改x,yx,y,其余置1;

我上来是暴力计算,结果超时,gg
#include <bits/stdc++.h>
using namespace std; #define st first
#define nd second
typedef long long LL;
typedef long double LLD;
typedef pair <int, int> PII;
typedef pair <LL, LL> PLL;
typedef pair <LLD, LLD> PLLD; struct Info
{
int L, R;
char Lc, Rc; Info (char Lc_, char Rc_) : L(), R(), Lc(Lc_), Rc(Rc_) {}
}; const int C = ;
int D[C]; int GetInt (char c)
{
return (int)(c - 'a');
} void Add (string s, int T[])
{
int cnt = ;
char c = s[]; for (int i = ; i < s.size(); ++i)
{
if (s[i] == s[i - ])
++cnt;
else
{
cnt = ;
c = s[i];
} int l = GetInt(c);
T[l] = max(T[l], cnt);
}
} Info GetInfo (string s)
{
Info info = Info(s[], s[s.size() - ]); int i = ;
while (i < s.size() && s[i] == s[i - ])
{
++info.L;
++i;
} i = s.size() - ;
while (i >= && s[i] == s[i + ])
{
++info.R;
--i;
} if (info.R == s.size())
info.R = ; return info;
} void Update (string s)
{
int T[C] = {};
for (int i = ; i < C; ++i)
if (D[i])
T[i] = ; Add(s, T); Info info = GetInfo(s); if (info.R == )
{
int l = GetInt(info.Lc), d = D[l];
T[l] = d + (d + ) * info.L;
}
else
{
int lL = GetInt(info.Lc), lR = GetInt(info.Rc);
T[lL] = info.L;
T[lR] = max(T[lR], info.R); if (lL == lR)
{
if (D[lL])
T[lL] = info.L + info.R + ;
}
else
{
if (D[lL])
T[lL] = max(T[lL], info.L + );
if (D[lR])
T[lR] = max(T[lR], info.R + );
}
} copy(T, T + C, D);
} int main ()
{
ios_base::sync_with_stdio(false); int n;
cin >> n; for (int i = ; i < n; ++i)
{
string s;
cin >> s;
Update(s);
} int result = ;
for (int i = ; i < C; ++i)
result = max(result, D[i]);
printf("%d", result); return ;
}
上面是大佬代码
E. String Multiplication的更多相关文章
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- CF 1131 E. String Multiplication
E. String Multiplication 题意 分析: 从后往前考虑字符串变成什么样子. 设$S_i = p_1 \cdot p_2 \dots p_{i}$,最后一定是$S_{n - 1} ...
- CF #541 E. String Multiplication
题意: 给定一系列字符串,每次都是后一个字符串和前面的融合,这个融合操作就是原来的串分成独立的,然后把新串插入到这些空格中.问最后,最长的相同连续的长度. 思路: 这道题可以贪心的来,我们压缩状态,记 ...
- CF1131E String Multiplication(???)
这题难度2200,应该值了. 题目链接:CF原网 题目大意:定义两个字符串 $s$ 和 $t$($s$ 的长度为 $m$)的乘积为 $t+s_1+t+s_2+\dots+t+s_m+t$.定义一个字符 ...
- JSP基础语法---九九乘法表-java jsp
<%@ page language="java" import="java.util.*" contentType="text/html; ch ...
- 2.Perl基础系列之入门
官网提供的入门链接:http://perldoc.perl.org/perlintro.html 语法概述 Perl的安装步骤省略,直接去官网下载并按照提示安装即可. 如果Perl安装没问题,那么运行 ...
- c++的正整数高精度加减乘除
数值计算之高精度加减乘除 一. 高精度正整数的高精度计算 1.加法 2.减法 减法和加法的最大区别在于:减法是从高位开始相减,而加法是从低位开始相加 3.乘法:用高精度加法实现 l 乘法的主 ...
- Xamarin.Android 入门实例(2)之实现WCF 寄宿于IIS 的Web服务提供
1.WCF 契约 ICalculator.cs using System.ServiceModel; namespace Contracts { [ServiceContract] public in ...
- Codeforces Round #541 (Div. 2) (A~F)
目录 Codeforces 1131 A.Sea Battle B.Draw! C.Birthday D.Gourmet choice(拓扑排序) E.String Multiplication(思路 ...
随机推荐
- 【FFT初识】
FFT在用于解决多项式乘法A*B(A和B为多项式,形如a0+a1*x^1+a2*x^2....)的时候,通俗地解释就是: 原理:先根据各自的系数各自转化为对应的向量(O(nlogn)),然后向量相 ...
- 【HNOI2004】 打鼹鼠
[题目链接] 点击打开链接 [算法] 动态规划 f[i]表示上一次打了第i只鼹鼠,所能打死的最多的鼹鼠数量 [代码] #include<bits/stdc++.h> using names ...
- 【OpenFOAM】——OpenFOAM入门算例学习
1 明确目标——为啥费老大劲儿学习OpenFOAM 学习OpenFOAM主要出于课题需要,希望实现以下几个目标: l [ ]学会用SnappyHexMesh生成高质量网格: l [ ]学习使用O ...
- 《Deep Learning Face Attributes in the Wild》论文笔记
论文背景: IEEE International Conference on Computer Vision 2015 Ziwei Liu1, Ping Luo1, Xiaogang Wang2, X ...
- Ubuntu 安装MTP驱动访问安卓设备(转载)
转自:http://www.ipython.me/ubuntu/how-to-connect-kindle-with-ubuntu.html 1.安装MTP工具集: mr_liu@i-it:~$ su ...
- PCB 铜皮(Surface)折线多边形扩大缩小实现(第一节)
继续铜皮多边形的相关的算法, 如何用代码实现多边形的扩大与缩小,这部份内容准备分为四节内容来讲解, 第一节,折线多边形的扩大缩小(不包含圆弧) 此篇讲第一节 第二节,带圆弧的多边形的扩大缩小 第三 ...
- Rails5终端基本命令
更新: 2017/06/10 更新: 2017/06/21 增加迁移文件查看履历命令: rails db:migrate:status 迁移文件生成 ...
- bzoj 4481: [Jsoi2015]非诚勿扰【期望+树状数组】
首先很容易计算对于一个如意郎君列表里有x个男性的女性,编号排第i位的男性被选的概率是 \[ p*(1-p)^{i-1}+p*(1-p)^{i-1+n}+p*(1-p)^{i-1+n}+- \] \[ ...
- bzoj 3143 [Hnoi2013]游走【高斯消元+dp】
参考:http://blog.csdn.net/vmurder/article/details/44542575 和2337有点像 设点u的经过期望(还是概率啊我也分不清,以下都分不清)为\( x[u ...
- bzoj 2111: [ZJOI2010]Perm 排列计数【树形dp+lucas】
是我想复杂了 首先发现大于关系构成了一棵二叉树的结构,于是树形dp 设f[i]为i点的方案数,si[i]为i点的子树大小,递推式是\( f[i]=f[i*2]*f[i*2+1]*C_{si[i]-1} ...