E. String Multiplication

题意

分析:

  从后往前考虑字符串变成什么样子。

  设$S_i = p_1 \cdot p_2 \dots p_{i}$,最后一定是$S_{n - 1} \cdot p_n$,就是将$S_{n-1}$每两个字符之间放入$p_n$。按照$p_n$分类讨论,那么最后有三种情况。

  设$p_n$的开头字符是$c0$,结尾字符是$c1$,包含开头的连续段的长度是$len0$,包含结尾的连续段的长度是$len1$。

  1、$c0 \neq c1$,那么答案可以是三个:(1).可以是$p_n$中最长的连续子段;(2).如果$S_{n-1}$中存在$c0$,$len0+1$;(3).如果$S_{n-1}$中存在$c1$,$len1+1$。

  2、$c0 = c1$,并且整个串不只有一种字符:如果$S_{n-1}$中存在$c0$,那么答案可以是$len0+len1+1$

  3、如果$p_n$只有一种字符构成,那么求$S_{n-1}$中最长的字符是$c0$连续段的,设为t,答案是$(t+1) \times len + t$。

  可以递归查询。

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
string s[N];
bool a[N][], b[N]; LL dfs(int x,char c) {
if (x == ) return ;
int ans = a[x - ][c - 'a'], sz = s[x].size();
if (b[x] && s[x][] == c) {
ans = dfs(x - , c);
return (ans + ) * sz + ans;
}
else {
int q = , h = , last = -;
for (int i = ; i < sz; ++i) {
if (s[x][i] == c && last == -) last = i;
if (s[x][i] == c) ans = max(ans, i - last + );
else last = -;
}
for (int i = ; i < sz; ++i) {
if (s[x][i] == c) q ++; else break;
}
for (int i = sz - ; ~i; --i) {
if (s[x][i] == c) h ++; else break;
}
if (s[x][] == c && s[x - ][sz - ] == s[x][] && a[x - ][c - 'a']) ans = max(ans, q + h + );
if (s[x][] == c && a[x - ][c - 'a']) ans = max(ans, q + );
if (s[x][sz - ] == c && a[x - ][c - 'a']) ans = max(ans, h + );
if (s[x][] == c) ans = max(ans, q);
if (s[x][sz - ] == c) ans = max(ans, h);
return ans;
}
}
int main() {
int n = read();
for (int i = ; i <= n; ++i) cin >> s[i], b[i] = ;
for (int i = ; i <= n; ++i) {
for (int j = ; j < (int)s[i].size(); ++j) if (s[i][j] != s[i][j - ]) { b[i] = ; break; }
for (int j = ; j < (int)s[i].size(); ++j) a[i][s[i][j] - 'a'] = ;
for (int j = ; j < ; ++j) a[i][j] |= a[i - ][j];
}
int ans = , q = , h = , last = , sz = s[n].size();
for (int i = ; i < sz; ++i) {
if (last == && s[n][i] == s[n][]) q ++;
if (s[n][i] == s[n][last]) ans = max(ans, i - last + );
else last = i;
}
for (int i = sz - ; i >= ; --i) {
if (s[n][i] == s[n][sz - ]) h ++;
else break;
}
if (!b[n]) {
if (s[n][] == s[n][sz - ] && a[n - ][s[n][] - 'a']) ans = max(ans, q + h + );
if (a[n - ][s[n][] - 'a']) ans = max(ans, q + );
if (a[n - ][s[n][sz - ] - 'a']) ans = max(ans, h + );
ans = max(ans, max(q, h));
cout << ans;
return ;
}
int t = dfs(n - , s[n][]);
cout << (t + ) * sz + t;
return ;
}

CF 1131 E. String Multiplication的更多相关文章

  1. CF #541 E. String Multiplication

    题意: 给定一系列字符串,每次都是后一个字符串和前面的融合,这个融合操作就是原来的串分成独立的,然后把新串插入到这些空格中.问最后,最长的相同连续的长度. 思路: 这道题可以贪心的来,我们压缩状态,记 ...

  2. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  3. E. String Multiplication

    E. String Multiplication time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  4. CF1131E String Multiplication(???)

    这题难度2200,应该值了. 题目链接:CF原网 题目大意:定义两个字符串 $s$ 和 $t$($s$ 的长度为 $m$)的乘积为 $t+s_1+t+s_2+\dots+t+s_m+t$.定义一个字符 ...

  5. CF 1003B Binary String Constructing 【构造/找规律/分类讨论】

    You are given three integers a, b and x. Your task is to construct a binary string s of length n=a+b ...

  6. CF 1140B Good String

    Description You have a string ss of length nn consisting of only characters > and <. You may d ...

  7. 【CF 710F】String Set Queries

    在校内OJ上A了,没有加强制在线的东西..不放链接了. 这道题题意是维护一个字符串集合,支持三种操作: 1.加字符串 2.删字符串 3.查询集合中的所有字符串在给出的模板串中出现的次数 操作数\(m ...

  8. CF 827E Rusty String FFT

    传送门 如果没有碍事的?的话,判定字符串的循环节直接用KMP的失配数组就可以搞定.现在有了碍事的?,我们就需要考虑更通用的算法. 考虑KMP失配数组判定字符串循环节的本质,发现判定\(k\)是否为字符 ...

  9. CF - 1131 D Gourmet choice

    题目传送门 先把 = 的人用并查集合并在一起. 然后 < > 的建边, 跑一遍 toposort 之后就好了. 入度为0点的值肯定为1, 然后就是因为这个是按照时间线走过来的,所以一个点的 ...

随机推荐

  1. 【比赛游记】FJOI2019瞎打记

    \(\mathrm{day}\) \(-4\) 又是睡到中午才起来,这样下去省选会睡迟的. 然后下午在补 WF2019 的题目,很快就能补完的(大雾). \(\mathrm{day}\) \(-3\) ...

  2. linux 同步IO: sync、fsync与fdatasync、sys_sync【转】

    本文转自:http://blog.csdn.net/cywosp/article/details/8767327 和 http://www.2cto.com/os/201204/126687.html ...

  3. haproxy配置基于ssl证书的https负载均衡

    本实验全部在haproxy1.5.19版本进行测试通过,经过测试1.7.X及haproxy1.3版本以下haproxy配置参数可能不适用,需要注意版本号. 一.业务要求现在根据业务的实际需要,有以下几 ...

  4. VM 安装 linux Enterprise_R5_U4_Server_I386_DVD教程图解

    ocp 学习笔记 20161126--------linux 笔记整理 一:安装linux系统环境: 1:linux 系统安装包下载路径:链接:链接: https://pan.baidu.com/s/ ...

  5. Java多线程中wait语句的具体使用技巧

    Java多线程在使用的时候会有很多语句需要我们具体的学习,在这其中wait()就是其中的一个.当然我们需要不断的努力学习才能掌握这一个语句的应用,下面的代码会对你学习Java多线程有所帮助. clas ...

  6. 前端工程化-webpack(babel编译ES6)

    最新版安装与普通安装 使用babel-loader编译ES6,需要遵循规范,安装babel-presets 规范列表 对应babel-loader,babel-preset安装最新版和普通版: pre ...

  7. php中按指定标识及长度替换字符的方法代码

    /** * 按指定标识及长度替换字符 * @param $str * @param int $start 开始的位数 * @param int $end 后面保留的位数 * @param string ...

  8. python简单笔记

    Remarks:python中注意缩进(Tab键或者4个空格) print(输出) 格式:print(values) 字符串.数字.变量等都可以输出: 实例: print(1)->1 print ...

  9. myEclipse配置java版本(环境、项目、编译)

    从别的地方导入一个项目的时候,经常会遇到eclipse/Myeclipse报Description  Resource Path Location Type Java compiler level d ...

  10. eclipse的操作

    IDEA至少在4G内存的电脑才能使用 eclipse中:项目名字小写 close project:关掉项目 删除未尽的项目导入eclipse中的步骤: 左边右键>>>import&l ...