Codeforces Round #541 (Div. 2) E 字符串 + 思维 + 猜性质
https://codeforces.com/contest/1131/problem/D
题意
给你n个字符串,字符串长度总和加起来不会超过1e5,定义字符串相乘为\(s*s1=s1+s[0]+s1+s[1]+s1+...+s1+s[size-1]+s1+s[size]+s1\),求n个字符串依次相乘后最长连续字符相同的子序列长度
题解
- 鬼畜的题意 or 难以优化的复杂度,都需要观察性质才能做,第二串要插入第一个串每个字符之间,可以看出字符数增长的速度很快,所以并不能把整个字符存下来
- 只看一种字符的话,最优选择肯定是将后面的串插进前面串最长子序列之间
- 分两种情况看,假设当前答案为len,cal()为计算当前字符串最长子序列函数,front()为最长前缀,back()为最长后缀
- 假如第二串是由同一字符组成,那么len=|s2|*(len+1)+len
- 反之,len=max(cal(s2),front(s2)+back(s2)+1),答案可能在为s2中,或者两个s2夹着一个s1的字符
代码
#include<bits/stdc++.h>
#define M 100005
using namespace std;
string s[M];
int i,j,n,tp,ans;
int ft(int p,char c){
int cnt=0;
for(int i=0;i<s[p].size();i++){
if(s[p][i]==c)cnt++;
else return cnt;
}
}
int bk(int p,char c){
int cnt=0;
for(int i=s[p].size()-1;i>=0;i--){
if(s[p][i]==c)cnt++;
else return cnt;
}
}
int cal(int p,char c){
int cnt=0,ans=0;
for(int i=0;i<s[p].size();i++){
if(s[p][i]==c)cnt++;
else cnt=0;
ans=max(ans,cnt);
}
return ans;
}
int ck(int p,char c){
for(int i=0;i<s[p].size();i++){
if(s[p][i]!=c)return 0;
}
return 1;
}
int main(){
cin>>n;
for(i=1;i<=n;i++)cin>>s[i];
for(i=0;i<26;i++){
tp=0;
for(j=1;j<=n;j++){
if(tp==0){
tp=cal(j,i+'a');
}else{
if(ck(j,i+'a'))
tp+=cal(j,i+'a')*(tp+1);
else
tp=max(cal(j,i+'a'),ft(j,i+'a')+bk(j,i+'a')+1);
}
/*if(tp>ans){
cout<<i<<" "<<j<<" "<<tp<<endl;
ans=max(ans,tp);
}
*/
}
ans=max(ans,tp);
}
cout<<ans;
}
Codeforces Round #541 (Div. 2) E 字符串 + 思维 + 猜性质的更多相关文章
- Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)
https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...
- Codeforces Round #541 (Div. 2)
Codeforces Round #541 (Div. 2) http://codeforces.com/contest/1131 A #include<bits/stdc++.h> us ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- Codeforces Round #410 (Div. 2)(A,字符串,水坑,B,暴力枚举,C,思维题,D,区间贪心)
A. Mike and palindrome time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...
- Codeforces Round #336 (Div. 2)【A.思维,暴力,B.字符串,暴搜,前缀和,C.暴力,D,区间dp,E,字符串,数学】
A. Saitama Destroys Hotel time limit per test:1 second memory limit per test:256 megabytes input:sta ...
- Codeforces Round #541 (Div. 2)题解
不知道该更些什么 随便写点东西吧 https://codeforces.com/contest/1131 ABC 太热了不写了 D 把相等的用并查集缩在一起 如果$ x<y$则从$ x$往$y$ ...
- Codeforces Round #541 (Div. 2) (A~F)
目录 Codeforces 1131 A.Sea Battle B.Draw! C.Birthday D.Gourmet choice(拓扑排序) E.String Multiplication(思路 ...
- Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)
D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
- Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序
https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...
随机推荐
- Delphi: TGraphicControl支持PaintTo方法
Delphi之TWinControl支持PaintTo方法,可以方便的Paint有句柄控件,而此方法,TGraphicControl没有. 这使得有时需要Paint无句柄控件诸如TLabel时颇为费事 ...
- c#: Noto Sans字体如何支持韩文
1.源起: VCU10项目,使用了Noto Sans字体,的确漂亮.但验证在win7下,其显示韩文为乱码,颇为头痛. 其界面显示如图: 度娘之,得Noto Sans又有CJK字体,顾名思义,其为支持中 ...
- 高盛oa
一道题根本不会,抄答案过了.一道自己写,莫名其妙出现了不会的bug.最后交了暴力解,过了5/7.估计要跪. 总结: 缺点:做过的不熟练.没做过的题不会.一个陌生的小bug也de不出来. 措施:多总结还 ...
- [leetcode]103. Binary Tree Zigzag Level Order Traversal二叉树来回遍历
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- 纯css实现div中未知尺寸图片的垂直居中
1.淘宝的方法 在曾经的"淘宝UED招聘"中有这样一道题目: “使用纯CSS实现未知尺寸的图片(但高宽都小于200px)在200px的正方形容器中水平和垂直居中.” 当然出题并不是 ...
- git diff 分支1 分支2 --stat命令没有将所有的不同显示出来
昨天遇到使用git diff 分支1 分支2 --stat命令时打印的文件修改列表,并不全,导致找了一下午的问题没有找到...特此记录,显示不全的原因我还没有找到,特此记录.
- DirectX中坐标系问题
自己属于笨类型,以前总是记不住directx坐标系是怎样的,今天在网上看到一篇文章,借鉴过来. 1. 明确DirectX是左手坐标系. 描述如下:1. 伸出左手,手面朝上,手背朝下,握住z轴,大拇指方 ...
- 18.Mysql SQL优化
18.SQL优化18.1 优化SQL语句的一般步骤 18.1.1 通过show status命令了解各种SQL的执行频率show [session|global] status; -- 查看服务器状态 ...
- springboot 项目添加jaeger调用链监控
1.添加maven依赖<dependency> <groupId>io.opentracing.contrib</groupId> <artifactId&g ...
- hdu 1686 & poj 2406 & poj 2752 (KMP入门三弹连发)
首先第一题 戳我穿越;http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意好理解,每组输入一个子串和一个母串,问在母串中有多少个子串? 文明人不要暴力 ...