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[] ...
随机推荐
- 【python】入门指南1
基础的数据结构:int, float, string 注意:python入门系列的文章的示例均使用python3来完成. #!/bin/python a = 1 b = 1.0 c = 'string ...
- 解决在Mac的Vmware Fusion中装win7系统和mac原生系统直接切换win7系统分辨率变化的问题
虚拟机 - 设置 - 显示屏 - 全屏显示retina (此选项钩去掉)
- xml添加新节点
#!/usr/bin/env python #coding:utf-8 # Author: xiaobaichuangtianxia--<> # Purpose: add jacoco d ...
- C#中泛型的解释(object,list,var,dynamic的区别)
泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能.泛型将类型参数的概念引入 .NET Framework,类型参数使得设计如下类和方法成为可能:这些类和方法将一个或多个类型的 ...
- input 原生上传文件(type = file)
1.表单上传文件的步骤: - 1)设置enctype 默认为:enctype="application/x-www-form-urlencoded"(一般不设置) 若要表单中有需要 ...
- vue2.0生命周期详解
首先上图展 <template> <div id="home"> <p>{{ message }}</p> </div> ...
- python any() all()
any() 函数用于判断给定的可迭代参数 iterable 是否全部为 False,则返回 False,如果有一个为 True,则返回 True. 元素除了是 0.空.FALSE 外都算 TRUE. ...
- 《大道至简》第一章--编程的精意 读后感(JAVA伪代码)
1. /*愚公移山 原始需求:惩山北之塞,出入之迂: 项目沟通:聚室而谋曰: 项目目标:毕力平险,指通豫南,达于汉阴: 技术方案:扣石垦壤,箕畚运于渤海之尾: 人员构成:愚公率子孙荷担者三夫,邻人京城 ...
- Android开发之动态设置字体的样式和粗细
字体设置通常有两种形式: 1:在xml中直接设置 android:textStyle="bold" android:typeface="sans" 2:用jav ...
- DOM-设置样式心得
一.style属性的设置和获取 style是一个对象,不能通过内嵌或外链获取,也就是只有是行内式的时候才能打印显示 style本身是一个对象 属性的值是字符串,没有赋值的情况下是"" ...