E. String Multiplication
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

Print exactly one integer — the beauty of the product of the strings.

Examples
input

Copy
3
a
b
a
output

Copy
3
input

Copy
2
bnn
a
output

Copy
1
Note

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的更多相关文章

  1. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  2. CF 1131 E. String Multiplication

    E. String Multiplication 题意 分析: 从后往前考虑字符串变成什么样子. 设$S_i = p_1 \cdot p_2 \dots p_{i}$,最后一定是$S_{n - 1} ...

  3. CF #541 E. String Multiplication

    题意: 给定一系列字符串,每次都是后一个字符串和前面的融合,这个融合操作就是原来的串分成独立的,然后把新串插入到这些空格中.问最后,最长的相同连续的长度. 思路: 这道题可以贪心的来,我们压缩状态,记 ...

  4. CF1131E String Multiplication(???)

    这题难度2200,应该值了. 题目链接:CF原网 题目大意:定义两个字符串 $s$ 和 $t$($s$ 的长度为 $m$)的乘积为 $t+s_1+t+s_2+\dots+t+s_m+t$.定义一个字符 ...

  5. JSP基础语法---九九乘法表-java jsp

    <%@ page language="java" import="java.util.*" contentType="text/html; ch ...

  6. 2.Perl基础系列之入门

    官网提供的入门链接:http://perldoc.perl.org/perlintro.html 语法概述 Perl的安装步骤省略,直接去官网下载并按照提示安装即可. 如果Perl安装没问题,那么运行 ...

  7. c++的正整数高精度加减乘除

    数值计算之高精度加减乘除 一.      高精度正整数的高精度计算 1.加法 2.减法 减法和加法的最大区别在于:减法是从高位开始相减,而加法是从低位开始相加 3.乘法:用高精度加法实现 l 乘法的主 ...

  8. Xamarin.Android 入门实例(2)之实现WCF 寄宿于IIS 的Web服务提供

    1.WCF 契约 ICalculator.cs using System.ServiceModel; namespace Contracts { [ServiceContract] public in ...

  9. Codeforces Round #541 (Div. 2) (A~F)

    目录 Codeforces 1131 A.Sea Battle B.Draw! C.Birthday D.Gourmet choice(拓扑排序) E.String Multiplication(思路 ...

随机推荐

  1. Evernote相关技术介绍——mysql+lucene+tomcat

    Evernote服务 我们的服务由以下几个组件组成. 分片(NoteStore)   分片是Evernote服务的核心单元,用于存储用户的笔记.每个分片最多可以支撑30万个Evernote用户,并包含 ...

  2. 【TJOI 2014】 上升子序列

    [题目链接] 点击打开链接 [算法] 先考虑50分的做法 : f[i]表示以i结尾的本质不同的上升子序列的个数 则f[i] = sigma(f[j]) (j < i,a[j] < a[i] ...

  3. 08_传智播客iOS视频教程_Foundation框架

    比如产生随机数.这个功能要你写吗?不用,因为苹果已经写好了.后面想开发一个ios程序,往界面上放一个按钮,实际上这个按钮不用你写别人已经写好了,你就拿过来拖一下就可以了. 框架是1个功能集 苹果或者第 ...

  4. Spring注解详细

    1.@controller 控制器(注入服务) 2.@service 服务(注入dao) 3.@repository dao(实现dao访问) 4.@component (把普通pojo实例化到spr ...

  5. Ruby  Numeric类

    Numeric类   Numeric ---------> Integer ---------> Fixnum                                       ...

  6. 洛谷 P4009 汽车加油行驶问题 【最小费用最大流】

    分层图,建k层,设(i,j,0)为点(i,j)的满油状态,全图的流量都是1,因为重复走到一个点没有意义.如果当前点是加油站,那么它向它上左的点连费用为a的边,向下右连费用为a+b的边: 否则,这个点的 ...

  7. CF896C Willem, Chtholly and Seniorious(珂朵莉树)

    中文题面 珂朵莉树的板子……这篇文章很不错 据说还有奈芙莲树和瑟尼欧里斯树…… 等联赛考完去学一下(逃 //minamoto #include<bits/stdc++.h> #define ...

  8. 使用python计算softmax函数

    softmax计算公式:                        Softmax是机器学习中一个非常重要的工具,他可以兼容 logistics 算法.可以独立作为机器学习的模型进行建模训练.还可 ...

  9. 《linux就该这么学》学习笔记

    本篇文章是根据刘遄老师的<linux就该这么学>中个人易忘知识点的读书笔记,结合的是个人弱点,可能不适合广大的网友同学,并在此声明本篇文章只是用于学习之用,绝无侵犯版权之意 linux就该 ...

  10. Oracle10g的imp命令

    Oracle10g使用imp命令导出数据为dmp:imp system/password@orcl file=rd_online_20181102.dmp fromuser=user1 touser= ...