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(思路 ...
随机推荐
- [Usaco2017 Dec] A Pie for a Pie
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5140 [算法] 最短路 时间复杂度 : O(N^2) [代码] #include&l ...
- 【转】zip() 函数
描述 zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表. 如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符 ...
- Java-Runoob-高级教程-实例-字符串:10. Java 实例 - 测试两个字符串区域是否相等-uncheck
ylbtech-Java-Runoob-高级教程-实例-字符串:10. Java 实例 - 测试两个字符串区域是否相等 1.返回顶部 1. Java 实例 - 测试两个字符串区域是否相等 Java ...
- body和html
1 关于html和body的背景颜色的一些变现 当给body设置背景颜色时(html没有背景颜色),这时body被当做根节点被浏览器俘获,浏览器界面的背景颜色就为body的background颜色:当 ...
- (转)Repeater中增加序号自增列
<%# Convert.ToString(Container.ItemIndex+)%> 当Repeater空为时,提示没有数据... <FooterTemplate> < ...
- Gym 100531B Buffcraft (贪心+暴力+前缀和)
题意:给定两个加血的方式,一个是直接加多少,另一种是加百分之几,然后你能够你选 k 种,问你选哪 k 种. 析:首先肯定要选加的多的,所以我们先排序,从大到小,然后用前缀和存储一下,再去枚举从第一种和 ...
- 2018SCin tsyzDay2 模拟赛-动态规划(简单的)
内心OS:简单?????还是我太弱了. 期望得分:100+100+0+0+0+0+随机暴力的点==200 实际得分:0+100+10+0+10+0==120 您知道我第一题为什么错了嘛??文件在混乱中 ...
- Permutation UVA - 11525(值域树状数组,树状数组区间第k大(离线),log方,log)(值域线段树第k大)
Permutation UVA - 11525 看康托展开 题目给出的式子(n=s[1]*(k-1)!+s[2]*(k-2)!+...+s[k]*0!)非常像逆康托展开(将n个数的所有排列按字典序排序 ...
- How many Fibs? POJ - 2413
How many Fibs? POJ - 2413 高精模板 #include<cstdio> #include<cstring> #include<algorithm& ...
- dotnetty源码解读一些要点
DefaultAttributeMap 它绑定在Channel或者ChannelHandlerContext上的一个附件. ChannelHandlerContext都是ChannelHandler和 ...