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. 在Spring(4.3.22)中集成Hibernate(5.4.0)

    (1)pom中添加相关依赖 <dependency> <groupId>org.hibernate</groupId> <artifactId>hibe ...

  2. ubuntu下tensorflow 报错 libcusolver.so.8.0: cannot open shared object file: No such file or directory

    解决方法1. 在终端执行: export LD_LIBRARY_PATH=”$LD_LIBRARY_PATH:/usr/local/cuda/lib64” export CUDA_HOME=/usr/ ...

  3. nodejs 使用http模块保存源码

    var xpath=require("xpath"); var fs=require("fs"); var dom = require('xmldom').DO ...

  4. centos环境无法安装paramiko的问题解决

    yum install openssl-devel yum install pycrypto yum install python-devel 全部安装完毕后执行pip install paramik ...

  5. (记录合并)union和union all的区别

    SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION内部的SELECT语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条SE ...

  6. 使用swiper.js实现移动端tab切换

    在项目中遇到的,要实现tab切换,我用的是swiper.js 官网:http://www.swiper.com.cn/api/start/new.html <!DOCTYPE html> ...

  7. 缓存之EHCache(一)

    源文: http://blog.csdn.net/l271640625/article/details/20528573 一.简介 非常简单,而且易用.     ehcache 是一个非常轻量级的缓存 ...

  8. 步步为营-20-XML

    说明:可扩展标记语言 eXtensible Markup Language--区分大小写 涉及到的知识点:DOM 文档对象模型 文本文件存储数据缺点:1,不易读取.2,易乱码 1 通过代码创建一个xm ...

  9. Android动态设置纯色图标的颜色

    https://blog.csdn.net/qq_20082961/article/details/73718363 以前做了一个流量悬浮窗,悬浮窗里有当前网络状态的图标和网速的文字,想实现改变文字颜 ...

  10. DbCommandInterceptor抓取EF执行时的SQL语句

    EF6.1也出来不少日子了,6.1相比6.0有个很大的特点就是新增了System.Data.Entity.Infrastructure.Interception 命名空间,此命名空间下的对象可以允许我 ...