CF 1131 E. String Multiplication
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的更多相关文章
- CF #541 E. String Multiplication
题意: 给定一系列字符串,每次都是后一个字符串和前面的融合,这个融合操作就是原来的串分成独立的,然后把新串插入到这些空格中.问最后,最长的相同连续的长度. 思路: 这道题可以贪心的来,我们压缩状态,记 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- E. String Multiplication
E. String Multiplication time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- CF1131E String Multiplication(???)
这题难度2200,应该值了. 题目链接:CF原网 题目大意:定义两个字符串 $s$ 和 $t$($s$ 的长度为 $m$)的乘积为 $t+s_1+t+s_2+\dots+t+s_m+t$.定义一个字符 ...
- 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 ...
- CF 1140B Good String
Description You have a string ss of length nn consisting of only characters > and <. You may d ...
- 【CF 710F】String Set Queries
在校内OJ上A了,没有加强制在线的东西..不放链接了. 这道题题意是维护一个字符串集合,支持三种操作: 1.加字符串 2.删字符串 3.查询集合中的所有字符串在给出的模板串中出现的次数 操作数\(m ...
- CF 827E Rusty String FFT
传送门 如果没有碍事的?的话,判定字符串的循环节直接用KMP的失配数组就可以搞定.现在有了碍事的?,我们就需要考虑更通用的算法. 考虑KMP失配数组判定字符串循环节的本质,发现判定\(k\)是否为字符 ...
- CF - 1131 D Gourmet choice
题目传送门 先把 = 的人用并查集合并在一起. 然后 < > 的建边, 跑一遍 toposort 之后就好了. 入度为0点的值肯定为1, 然后就是因为这个是按照时间线走过来的,所以一个点的 ...
随机推荐
- Vim中自动在程序起始处添加版权和作者信息
在编写程序的时候,经常需要在程序开始写上程序的简要介绍和作者信息,如下: 这种信息,除了文件名和修改时间可能经常发生变化外,其他基本不变,可以在程序开始自动加入,方法就是在家目录下的.vimrc中写入 ...
- opencv入门指南(转载)
转载链接:http://blog.csdn.net/morewindows/article/details/8426318 网上的总结的一些用openncv的库来做的事: 下面列出OpenCV入门指南 ...
- Linux mmc framework2:基本组件之queue
1.前言 本文主要介绍card下queue组件的主要流程,在介绍的过程中,将详细说明和queue相关的流程,涉及到其它组件的详细流程再在相关文章中说明. 2.主要数据结构和API 2.1 struct ...
- 异步编程之使用yield from
异步编程之使用yield from yield from 是 Python3.3 后新加的语言结构.yield from的主要功能是打开双向通道,把最外层的调用方法与最内层的子生成器连接起来.这两者就 ...
- 记录entityframework生成的sql语句
Interceptors (EF6.1 Onwards) Starting with EF6.1 you can register interceptors in the config file. I ...
- Cleaning up old NVIDIA driver files
原文地址:https://www.gameplayinside.com/optimize/cleaning-up-old-nvidia-driver-files-to-save-disk-space/ ...
- SHA算法:签名串SHA算法Java语言参考(SHAHelper.java)
SHAHelper.java package com.util; /** * @author wangxiangyu * @date:2017年10月16日 上午9:00:47 * 类说明:SHA签名 ...
- dblink 退出 session
以dblink的表现为例,我一直认为dblink的远程连接session仅在操作(select,dml)发生时短期存在,在操作完成后依据一定条件保留或退出. 而事实并非如此,随便使用一个远程查询语句如 ...
- 基于TLS的EAP 认证方法
TLS: transport level security , 安全传输层协议,用于在两个通信应用程序之间提供保密性和数据完整性.该协议由两层组成: TLS 记录协议(TLS Record)和 TLS ...
- Web Services基础学习(W3C)
1.Web services 使用 XML 来编解码数据,并使用 SOAP 来传输数据 2.基础的 Web Services 平台是 XML + HTTP. Web services 平台的元素: S ...