CF #541 E. String Multiplication
题意:
给定一系列字符串,每次都是后一个字符串和前面的融合,这个融合操作就是原来的串分成独立的,然后把新串插入到这些空格中。问最后,最长的相同连续的长度。
思路:
这道题可以贪心的来,我们压缩状态,记录串中每个字母对应最长的长度。然后分类讨论处理就行了。
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> /* ⊂_ヽ
\\ Λ_Λ 来了老弟
\('ㅅ')
> ⌒ヽ
/ へ\
/ / \\
レ ノ ヽ_つ
/ /
/ /|
( (ヽ
| |、\
| 丿 \ ⌒)
| | ) /
'ノ ) Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/
const ll big = 1e9+;
ll dp[],tmp[];
string str;
int main(){
int n;
scanf("%d", &n);
ll ans = ;
rep(cc, , n){ cin>>str; int len = str.length();
int flag = ;
for(int i=; i<len; i++) if(str[i] != str[]) flag = ; if(flag) {
int id = (str[] - 'a');
rep(i, , ) {
if(i == id) continue;
if(dp[i]) dp[i] = ;
} if(dp[id]){
ll t = min(big, 1ll*(dp[id] + ) * len + dp[id]);
dp[id] = max(dp[id], t);
}
dp[id] = max(1ll*len, dp[id]);
}
else {
rep(i, , ){
if(dp[i]) dp[i] = ;
tmp[i] = ;
}
ll e = ;char la = str[];
str+="A";
rep(i, , len){
if(str[i] != la){
int id = (int)(la - 'a');
tmp[id] = max(tmp[id], e);
// debug(e);
la = str[i];
e = ;
}
else e++;
}
ll c1 = ,c2 = ;
for(int i=; i<len && str[i] == str[]; i++) c1++;
for(int i=len-; i>= && str[i] == str[len-];i--) c2++; if(str[] == str[len-]) {
int id = (int)(str[] - 'a');
if(dp[id]) dp[id] = min(big, max(dp[id], 1ll + c1 + c2));
}
else {
int id = (int)(str[] - 'a');
if(dp[id]) dp[id] = min(big, max(dp[id], 1ll + c1));
id = (int) (str[len-] - 'a');
if(dp[id]) dp[id] = min(big, max(dp[id], 1ll + c2));
}
rep(i, , ) {
dp[i] = max(dp[i], tmp[i]);
}
}
} rep(i, , ) ans = max(ans, dp[i]);
printf("%lld\n", ans);
return ;
}
CF #541 E. String Multiplication的更多相关文章
- CF 1131 E. String Multiplication
E. String Multiplication 题意 分析: 从后往前考虑字符串变成什么样子. 设$S_i = p_1 \cdot p_2 \dots p_{i}$,最后一定是$S_{n - 1} ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
- leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...
- leadcode 541. Reverse String II
package leadcode; /** * 541. Reverse String II * Easy * 199 * 575 * * * Given a string and an intege ...
- E. String Multiplication
E. String Multiplication time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 【leetcode_easy】541. Reverse String II
problem 541. Reverse String II 题意: 给定一个字符串,每隔k个字符翻转这k个字符,剩余的小于k个则全部翻转,否则还是只翻转剩余的前k个字符. solution1: cl ...
- CF1131E String Multiplication(???)
这题难度2200,应该值了. 题目链接:CF原网 题目大意:定义两个字符串 $s$ 和 $t$($s$ 的长度为 $m$)的乘积为 $t+s_1+t+s_2+\dots+t+s_m+t$.定义一个字符 ...
- [LeetCode&Python] Problem 541. Reverse String II
Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...
随机推荐
- Linux 文件系统相关的基本概念
本文介绍 Linux 文件系统相关的基本概念. 硬盘的物理组成 盘片硬盘其实是由单个或多个圆形的盘片组成的,按照盘片能够容纳的数据量,分为单盘(一个硬盘里面只有一个盘片)或多盘(一个硬盘里面有多个盘片 ...
- PID算法资料【视频+PDF介绍】
最近一直有网友看到我的博客后,加我好友,问我能不能给发一些PID的资料,今天找了一些资料放到百度网盘上,给大家下载: 视频资料 链接:https://pan.baidu.com/s/12_IlLgBI ...
- 经典SQL(sqlServer)
一.基础 .说明:创建新表create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..) .分组: ...
- 用python实现九九乘法表输出-两种方法
2019-08-05 思考过程:九九乘法表需要两层循环,暂且称之为内循环和外循环,因此需要写双层循环来实现. 循环有for和while两种方式. for循环的实现 for i in range(1,1 ...
- Tomcat+MySQL常见调优参数
一.Tomcat 调优 (一).Tomcat内存优化 参数一: vim /tomcat/bin/catalina.sh CATALINA_OPTS="-server -Xms128m -Xm ...
- RBF神经网络
RBF神经网络 RBF神经网络通常只有三层,即输入层.中间层和输出层.其中中间层主要计算输入x和样本矢量c(记忆样本)之间的欧式距离的Radial Basis Function (RBF)的值,输出层 ...
- 100天搞定机器学习|Day16 通过内核技巧实现SVM
前情回顾 机器学习100天|Day1数据预处理100天搞定机器学习|Day2简单线性回归分析100天搞定机器学习|Day3多元线性回归100天搞定机器学习|Day4-6 逻辑回归100天搞定机器学习| ...
- Map集合的遍历.
package collction.map; import java.util.HashMap; import java.util.Iterator; import java.util.Map; im ...
- 并查集模板题----P3367 【模板】并查集
题目描述 如题,现在有一个并查集,你需要完成合并和查询操作. 输入格式 第一行包含两个整数N.M,表示共有N个元素和M个操作. 接下来M行,每行包含三个整数Zi.Xi.Yi 当Zi=1时,将Xi与Yi ...
- zookeeper和dubbo安装与搭建
Zookeeper+Dubbo安装与搭建 (原创:黑小子-余) 本文有借鉴:https://www.cnblogs.com/UncleYong/p/10737119.html (一)zookeeper ...