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[] ...
随机推荐
- mybatis 返回类型为 java.lang.String 接收为null的情景
<select id="selectOnly" parameterType="java.util.Map" resultType="java.l ...
- Mac git 终端使用
终端有这个提示,这个按照命令 输入你的 git账号和邮箱就可以, 不然一直出这个提示 Your name and email address were configured automatically ...
- git删除提交历史
精准入口==>官方文档 Q:我们在提交代码时,把密码或者敏感信息也提交了,怎么办? A:“在本地删除密码或敏感信息后,再push到远程仓库” Q:"但这样删除后,在仓库的commit历 ...
- AngularJS——第6章 作用域
第6章 作用域 在AngularJS中使用过滤器格式化展示数据,在"{{}}"中使用"|"来调用过滤器,使用":"传递参数. 6.1 内置过 ...
- tomcat-maven-plugin的使用
maven有一个把web应用部署到tomcat下的插件 tomcat-maven-plugin , 我们可以使用这个插件把web应用一键式的部署到一个远程的tomcat中. 插件的url: http: ...
- eval详解
eval()的作用 把字符串参数解析成js代码并运行,并返回执行的结果: eval的作用域 作用域在它所有的范围内容有效 IE8及以下指向window 解决方法: functiona(){ if(wi ...
- Java反射获取对象VO的属性值(通过Getter方法)
有时候,需要动态获取对象的属性值. 比如,给你一个List,要你遍历这个List的对象的属性,而这个List里的对象并不固定.比如,这次User,下次可能是Company. e.g. 这次我需要做一个 ...
- js去除字符串空格(空白符)
使用js去除字符串内所带有空格,有以下三种方法: ( 1 ) replace正则匹配方法 去除字符串内所有的空格:str = str.replace(/\s*/g,""); 去除字 ...
- 学习knockoutjs轻量级的MVVM框架
教程:knockoutjs介绍 http://www.w3cfuns.com/forum.php?mod=viewthread&tid=5598714 MVVM架构~knockoutjs实现简 ...
- Find Amir CodeForces 805C
http://codeforces.com/contest/805/problem/C 题意:有n个学校,学校的编号是从1到n,从学校i到学校j的花费是(i+j)%(n+1),让你求遍历完所有学校的最 ...