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 443. String Compression

    下面反向遍历,还是正向好. void left(vector<char>& v, bool p(int)) { ; ; ; while (del < max_index) { ...

  2. 【python-sql】sql操作时遇到的坑

    针对python的sql语句的封装以及调用: python的db操作类: # -*- coding: utf-8 -*- # coding=utf-8 import MySQLdb class Dat ...

  3. day 22 反射,双下方法

    反射: 反射:通过字符串去操作对象(类,空间等等)的属性或方法反射的四个方法 hasattr *** getattr *** setattr *** delattr *** # getattr 用法c ...

  4. PHP统计网站pv(访问量)

    //首先判断有没有统计的文件 if(is_file("pv.txt")){//有 //取文件里面的值 $count=file_get_contents("pv.txt&q ...

  5. java 与 或 非 异或 & | ~ ^

    1.与运算符 & 两个操作数中位,否则结果为0 2.或运算符 | 两个位,否则就为0 3.非运算符 ~ 如果位为,结果是,如果位为1,结果是0 4.异或运算符 ^ 两个操作数的位中,,不同则结 ...

  6. opencv 3.2图像矩(Image Moments)

    图像矩乍看比较难理解,看了很多资料,大概明白了一些,但还是无法在脑海里形成一个模型概念,于是从源码中寻找它的应用. 今天就通过公式和程序抓取数据,来进一步理解图像矩 先看一个图片 这是程序运行结果, ...

  7. Oracle_高级功能(3) synonym和database link

    一.同义词synonymconnect sys/123 as sysdba;select * from emp;ORA-00942: 表或视图不存在create synonym emp for sco ...

  8. XiaoKL学Python(E)Generator Expressions

    在 阅读 https://github.com/vitonzhang/objc_dep 中的 objc_dep.py 时遇到: objc_files = (f for f in files if f. ...

  9. Luogu 1273 有线电视网 - 树形背包

    Description 树形背包, 遍历到一个节点, 枚举它的每个子节点要选择多少个用户进行转移. Code #include<cstring> #include<cstdio> ...

  10. PHP学习笔记(一)

    1.什么是 PHP? PHP 指 PHP:超文本预处理器(译者注:PHP: Hypertext Preprocessor,递归命名) PHP 是一种服务器端的脚本语言,类似 ASP PHP 脚本在服务 ...