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 字符串 + 思维 + 猜性质的更多相关文章

  1. Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)

    https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...

  2. Codeforces Round #541 (Div. 2)

    Codeforces Round #541 (Div. 2) http://codeforces.com/contest/1131 A #include<bits/stdc++.h> us ...

  3. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  4. 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 ...

  5. 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 ...

  6. Codeforces Round #541 (Div. 2)题解

    不知道该更些什么 随便写点东西吧 https://codeforces.com/contest/1131 ABC 太热了不写了 D 把相等的用并查集缩在一起 如果$ x<y$则从$ x$往$y$ ...

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

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

  8. Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)

    D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: =  的情况我们用并查集把他们扔到一个集合,然后根据 > ...

  9. Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序

    https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...

随机推荐

  1. leetcode输入输出加速

    C++兼容C的输入输出,即cin与scanf混用文件指针不会出错,cout亦同,导致cin有额外开销. 可以用std::ios::sync_with_stdio(false);手动关闭. cin.ti ...

  2. [leetcode]528. Random Pick with Weight按权重挑选索引

    Given an array w of positive integers, where w[i] describes the weight of index i, write a function  ...

  3. Linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结(转)

    本文介绍了linux下的压缩程式tar.gzip.gunzip.bzip2.bunzip2.compress .uncompress. zip. unzip.rar.unrar等程式,以及如何使用它们 ...

  4. linux命令学习之:tar

    tar命令可以为linux的文件和目录创建档案.利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件.tar最初被用来在磁带上创建档案,现在,用户可以在 ...

  5. python 学习笔记 ---- 数据类型

    Python有五个标准的数据类型: Numbers(数字) String(字符串) List(列表) Tuple(元组) Dictionary(字典) ① List 列表 和 Tuple 元组     ...

  6. ios证书安装和打包流程

    iOS开发流程 1.拿到源文件 2文件目录大致名字 一.证书配置 参考网站:http://www.jianshu.com/p/9d9e3699515e    (证书配置参考地址) 准备工作 首先要有苹 ...

  7. u-boot之怎么实现分区

    启动参数bootcmd=nand read.jffs2 0x30007FC0 kernel; bootm 0x30007FC0中kernel在哪定义,为什么可以直接引用?针对这个问题展开思考最终定位到 ...

  8. Activity(活动)

  9. BPF+XDP比较全的资料都在这里

    Dive into BPF: a list of reading material Sep 1, 2016 • Quentin Monnet◀Table of contents What is BPF ...

  10. Array Division 808D

    D. Array Division time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...