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(思路 ...
随机推荐
- 一场由过滤器Filter引发的血案
一场由过滤器Filter引发的血案 事件起因 本来应该是下图的登录界面 变成了这样 What's the fuck????? 抓狂 原因 解决方法: 在过滤器中给资源文件开个绿色通道
- python 列表,元祖,字典
一 列表 1 列表的循环遍历 namesList = ['xiaoWang','xiaoZhang','xiaoHua'] for name in namesList: print(name) 结果 ...
- Html.PartialView(),html.Renderpartial,html.action.html.RenderAction 辅助方法
Html.Partial(), 返回HTML字符串 .参数为部分视图 html.RenderPartial(),不返回返回HTML字符串 ,直接输出响应流.参数为部分视图 一般用于主视图中已经存在了这 ...
- Ubuntu 安装MTP驱动访问安卓设备(转载)
转自:http://www.ipython.me/ubuntu/how-to-connect-kindle-with-ubuntu.html 1.安装MTP工具集: mr_liu@i-it:~$ su ...
- E20180305-hm-xa
raw adj. 生的,未加工的; 无经验的; 新近完成的; 发炎的,疼痛的; payload n. 有效载荷; (航天器.卫星的) 装备; (车辆等的) 装载货物; (炸弹.导弹的) 爆炸力;
- Swift4 基本数据类型(范围型, Stride型, 数组, 字符串, 哈希表)
创建: 2018/02/28 完成: 2018/03/04 更新: 2018/05/03 给主要标题加上英语, 方便页内搜索 [任务表]TODO 范围型(Range)与Stride型 与范围运算符相 ...
- [App Store Connect帮助]七、在 App Store 上发行(3.3)提交至“App 审核”:解决 App 拒绝问题
如果“App 审核”或“Beta 版 App 审核”拒绝了您的 App,您可以与 Apple 沟通并在解决方案中心中解决问题. 来自 Apple 的通讯信息包含有关 App 拒绝的信息,其中包括该 A ...
- SpringMVC异步调用,Callable和DeferredResult的使用
Callable和DeferredResult都是springMVC里面的异步调用,Callable主要用来处理一些简单的逻辑,DeferredResult主要用于处理一些复杂逻辑 1.Callabl ...
- -------Pokemon Master------很水-------
A - Pokemon Master Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Sta ...
- Python字符串对象常用方法
安利一句话:字符串是不可变的对象,所以任何操作对原字符串是不改变的! 1.字符串的切割 def split(self, sep=None, maxsplit=-1): # real signature ...