Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication
Time Limit: 2000 mSec
Problem Description
Input
Output
Print exactly one integer — the beauty of the product of the strings.
Sample Input
3
a
b
a
Sample Output
3
题解:这个题的思维难度其实不大,需要维护什么东西很容易想到,或者说套路十分明显
1、字符串无限制条件下最大值
2、强制选择左端点/右端点的最大值
3、是否只有一种字符
基本上需要维护的量就是这些,不过写起来实在是非常麻烦,以我的代码能力短时间实在是写不出来,后来参考了一位大佬的代码,写的思路清晰,代码简洁,实在是非常值得学习,之所以能够使得代码变简洁,主要是因为他枚举了字母。题目明确说明只有小写英文字母,所以最后的最大值无非就是这26个字母中的一个形成的,因此分别计算各个字母对应的最大值即可算出最终最大值,而一旦固定了每次考虑取最大值的字母,写代码的难度会大大降低,关键点就这一条,直接上代码。
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 1; i <= (n); i++)
#define sqr(x) ((x) * (x)) const int maxn = + ;
const int maxm = + ;
const int maxs = + ; typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd; const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const double inf = 1e15;
const double pi = acos(-1.0); int n;
int len[maxn];
string str[maxn]; int main()
{
ios::sync_with_stdio(false);
cin.tie();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin >> n;
for (int i = ; i <= n; i++)
{
cin >> str[i];
len[i] = str[i].size();
}
int ans = ;
for (int c = ; c < ; c++)
{
int mx = , cnt = ;
for (int i = ; i < len[]; i++)
{
if (str[][i] - 'a' != c)
{
mx = max(mx, cnt);
cnt = ;
}
else
{
cnt++;
}
}
mx = max(mx, cnt);
for (int i = ; i <= n; i++)
{
int lmx = , rmx = ;
int nmx = ;
for (int j = ; j < len[i]; j++)
{
if (str[i][j] - 'a' == c)
lmx++;
else
break;
}
for (int j = len[i] - ; j >= ; j--)
{
if (str[i][j] - 'a' == c)
rmx++;
else
break;
}
cnt = ;
for (int j = ; j < len[i]; j++)
{
if (str[i][j] - 'a' != c)
{
nmx = max(nmx, cnt);
cnt = ;
}
else
cnt++;
}
nmx = max(nmx, cnt); if (nmx == len[i])
{
mx = (mx + ) * nmx + mx;
}
else
{
if (mx != )
mx = lmx + rmx + ;
else
mx = max(lmx, rmx);
}
mx = max(mx, nmx);
}
ans = max(ans, mx);
}
cout << ans << endl;
return ;
}
Codeforces #541 (Div2) - E. String Multiplication(动态规划)的更多相关文章
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- 【CF1132F】Clear the String(动态规划)
[CF1132F]Clear the String(动态规划) 题面 CF 题解 考虑区间\(dp\). 增量考虑,每次考虑最后一个字符和谁一起删去,然后直接转移就行了. #include<io ...
- CF 1131 E. String Multiplication
E. String Multiplication 题意 分析: 从后往前考虑字符串变成什么样子. 设$S_i = p_1 \cdot p_2 \dots p_{i}$,最后一定是$S_{n - 1} ...
- E. String Multiplication
E. String Multiplication time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- 【动态规划】【最短路】Codeforces 710E Generate a String
题目链接: http://codeforces.com/problemset/problem/710/E 题目大意: 问写N个字符的最小花费,写一个字符或者删除一个字符花费A,将当前的字符数量翻倍花费 ...
随机推荐
- [AI分享]零高数理解人工智能和深度学习
- python爬虫之selenium、phantomJs
图片懒加载技术 什么是图片懒加载技术 图片懒加载是一种网页优化技术.图片作为一种网络资源,在被请求时也与普通静态资源一样,将占用网络资源,而一次性将整个页面的所有图片加载完,将大大增加页面的首屏加载时 ...
- 如何接入银联“快速接入”产品API
引言:使用银联开放平台的用户或多或少都接触过产品API吧,那么大家对于“快速接入”产品API是否还会存在一些疑问呢?因为我之前对“快速接入”模糊不清,所以整理的一份详细的资料,里面梳理了“快速接入”产 ...
- 用ASP.NET Core 2.0 建立规范的 REST API -- DELETE, UPDATE, PATCH 和 Log
本文所需的一些预备知识可以看这里: http://www.cnblogs.com/cgzl/p/9010978.html 和 http://www.cnblogs.com/cgzl/p/9019314 ...
- 发布 ASP.NET Core 2.x 应用到 Ubuntu
简单绍一下如何将ASP.NET Core 应用发布到Linux (Ubuntu)服务器上,都是文档的东西. 服务器结构 ASP.NET Core 2.x 有两种server: HTTP.sys 只支持 ...
- Elasticsearch Index模块
1. Index Setting(索引设置) 每个索引都可以设置索引级别.可选值有: static :只能在索引创建的时候,或者在一个关闭的索引上设置 dynamic:可以动态设置 1.1. S ...
- Spring Boot Web 开发注解篇
本文提纲 1. spring-boot-starter-web 依赖概述 1.1 spring-boot-starter-web 职责 1.2 spring-boot-starter-web 依赖关系 ...
- XPath和CssSelector定位总结
1. 介绍XPath和CssSelector 2. XPath有哪些方式 2.1 通过XPath语法 2.2 Contains关键字 2.3 Start-With 2.4 Or和And关键字 2.5 ...
- Java Web开发模式
一 Java Web开发模式的变迁 1 最初的Java web服务器端编程技术是Servlet,利用Servlet就可以开发出一个Web应用程序. 2 为了解决Servlet缺陷,SUN推出了JSP技 ...
- 简单介绍shell编程四剑客之awk
概要:分别的作用 grep:文本过滤(模式:pattern)工具,grep,egrep,fgrep,擅长过滤. sed:stream editor 文本编辑工具:(流编辑器),擅长取行.替换. awk ...