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(思路 ...
随机推荐
- 稳定婚姻(tarjan)
传送门 这道题一开始可能以为是二分图匹配……?不过后来发现和二分图没啥大关系. 简单分析之后发现,把夫妻之间连边(男性向女性连边),之后再将每对曾经是情侣的人连边(女性向男性连边),当然以上的方向可以 ...
- Linux共享内存(二)
Linux共享内存编程实例 原文链接:http://blog.csdn.net/pcliuguangtao/article/details/6526119 /*共享内存允许两个或多个进程进程共享同一块 ...
- RobotFrameWork--selenium2模拟chrome的user agent
${options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver ${opt ...
- hibernate映射文件set key one-to-many
转自:https://www.linuxidc.com/Linux/2013-11/92228.htm 1 hibernate映射文件set key one-to-many的配置. POJOs如下: ...
- (转)Silverlight_5_Toolkit_December_2011 安装后点击Toolkit Samples没反应的解决方法
Silverlight Toolkit官方下载地址: http://silverlight.codeplex.com/ http://blog.csdn.net/hcj116/article/deta ...
- 文件cp功能
#include<stdio.h> #include<unistd.h> #include<fcntl.h> #include<string.h> in ...
- JavaScript编程艺术-第7章代码汇总(1)
1.document.write()(HTML与JS未分离) HTML: JS: 2..innerHTML(直接覆盖) HTML: JS: 3.getAttribute.setAttribute.ge ...
- [Usaco2017 Feb]Why Did the Cow Cross the Road I (Gold)
Description 有一幅n*n的方格图,n <=100,每个点上有一个值. 从(1,1)出发,走到(n,n),只能走上下左右. 每走一步花费t,每走三步需要花费走完三步后到达格子的值. 求 ...
- Miller&&Pollard HDOJ 4344 Mark the Rope
题目传送门 题意:一个长为n(n<2^63)的管子,在管子上做标记,每隔L个长度单位做一个标记,从管子头端开始,保证最后一次标记恰好在管子的尾端.让你找出有多少个这样的L(L<n),且他们 ...
- 题解报告:hdu1202The calculation of GPA(算绩点问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1202 Problem Description 每学期的期末,大家都会忙于计算自己的平均成绩,这个成绩对 ...