CH5702 Count The Repetitions
题意
5702 Count The Repetitions 0x50「动态规划」例题
描述
定义 conn(s,n) 为 n 个字符串 s 首尾相接形成的字符串,例如:
conn("abc",2)="abcabc"
称字符串 a 能由字符串 b 生成,当且仅当从字符串 b 中删除某些字符后可以得到字符串 a。例如“abdbec”可以生成“abc”,但是“acbbe”不能生成“abc”。
给定两个字符串 s_1 和 s_2,以及两个整数 n_1 和 n_2,求一个最大的整数 m,满足conn(conn(s_2,n_2 ),m) 能由 conn(s_1,n_1) 生成。
s_1 和 s_2 长度不超过100,n_1 和 n_2 不大于 10^6。
输入格式
本题只有1个测试点,包含多组数据。每组数据由2行组成,第一行是s2,n2,第二行是s1,n1。
输出格式
对于每组数据输出一行表示答案m。
样例输入
ab 2
acb 4
acb 1
acb 1
aa 1
aaa 3
baab 1
baba 11
aaaaa 1
aaa 20
样例输出
2
1
4
7
12
来源
https://leetcode.com/problems/count-the-repetitions/description/
</article>
分析
发现可以求出最大的m',满足conn(s2,m')能由conn(s1,n1)生成,那么答案m=m'/n2。
由于m'≤1e8不能直接F[i,j]表示从s1[i]开始要多少字符生成conn(s2,j),观察到m'可以用二进制拆分,所以利用倍增优化。
设F[i,j]表示从s1[i]开始至少需要多少字符才能生成conn(s2,\(2^j\))。状态转移方程为:
\]
可以暴力预处理F[i,0]。总时间复杂度\(O(|s_2||s_1|^2+|s_1|\log(|s_1|*n_1/|s_2|))\)
代码
#include<bits/stdc++.h>
#define rg register
#define il inline
#define co const
typedef long long ll;
using namespace std;
string s1,s2;
int n1,n2;
ll f[100][28];
void Count_the_Repetitions(){
for(int i=0;i<s1.size();++i){
int pos=i;
f[i][0]=0;
for(int j=0;j<s2.size();++j){
int cnt=0;
while(s1[pos]!=s2[j]){
pos=(pos+1)%s1.size();
if(++cnt>=s1.size()) return cout<<0<<endl,void();
}
pos=(pos+1)%s1.size(),f[i][0]+=cnt+1;
}
}
for(int j=1;j<=27;++j)
for(int i=0;i<s1.size();++i)
f[i][j]=f[i][j-1]+f[(i+f[i][j-1])%s1.size()][j-1];
ll m=0;
for(int st=0;st<s1.size();++st){
ll x=st,ans=0;
for(int k=27;k>=0;--k)
if(x+f[x%s1.size()][k]<=s1.size()*n1)
x+=f[x%s1.size()][k],ans+=1<<k;
m=max(m,ans);
}
cout<<m/n2<<endl;
}
int main(){
ios::sync_with_stdio(0);
while(cin>>s2>>n2>>s1>>n1) Count_the_Repetitions();
return 0;
}
CH5702 Count The Repetitions的更多相关文章
- CH5702 Count The Repetitions[倍增dp]
http://contest-hunter.org:83/contest/0x50%E3%80%8C%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%E3%80%8D%E4%B ...
- [Swift]LeetCode466. 统计重复个数 | Count The Repetitions
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- 466. Count The Repetitions
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- [LeetCode] Count The Repetitions 计数重复个数
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- Leetcode: Count The Repetitions
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- 【leetcode 字符串】466. Count The Repetitions
https://leetcode.com/problems/count-the-repetitions/description/ 找循环节 https://www.cnblogs.com/grandy ...
- 第七周 Leetcode 466. Count The Repetitions 倍增DP (HARD)
Leetcode 466 直接给出DP方程 dp[i][k]=dp[i][k-1]+dp[(i+dp[i][k-1])%len1][k-1]; dp[i][k]表示从字符串s1的第i位开始匹配2^k个 ...
- [LeetCode] 466. Count The Repetitions 计数重复个数
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- LeetCode466. Count The Repetitions
题目链接 传送门 题意 定义一个特殊的串, 现在给出串S1和S2的参数, 问: S2最多可以多少个连接起来扔是S1的子序列, 求出这个最大值 解题思路 注意s1与S1的区别, 可以去看题目描述, 预处 ...
随机推荐
- freecodecamp数字转化成罗马数字
做数字转罗马数字时,用到了贪心算法,很好用,记录一下,有时间系统的学一下 罗马数字的规则: 罗马数字网址 1 5 10 50 100 500 1000 I V X L C D M1 1当一个符号在一个 ...
- jquery将表单序列化
在工作中经常要将表单数据通过ajax提交,所以需要将表单序列化为json对象. 已经有大神提供了,以前一直百度,现在决定抄过来收藏一下,方便以后自己用,尊重原创,文章转载自:http://www.cn ...
- 英语发音规则---E字母常见的发音组合有哪些
英语发音规则---E字母常见的发音组合有哪些 一.总结 一句话总结:很好记的e和5个元音字母的组合,加一个非e开头的ie e:开音节 /i:/ eve /i:v/ n. 夏娃----闭音节 /e/ ...
- obs studio 使用
专业,开源,无广告,免费,录屏/推流神器--obs studio 稍微简单的也有captura, 原理:调用本地API获取音频流,图像流(全屏幕,单个windows窗口的图像输出)->开源音视频 ...
- [easyUI] datagrid 数据格 可以进行分页
1. 新建一个GridNode的类: public class GridNode { private Long id; private String title;//投票标题 private Inte ...
- 数据分析库之matplotlib
一.Matplotlib基础知识 Matplotlib中的基本图表包括的元素 x轴和y轴 axis 水平和垂直的轴线 轴标签 axisLabel 水平和垂直的轴标签 x轴和y轴刻度 tick 刻度标示 ...
- scrapy---反爬虫
反爬虫措施1)动态修改User-Agent2)动态修改ip3)延迟DOWNLOAD_DELAY = 0.5 1)在middleware中新建一个类,从fake_useragent中导入UserAgen ...
- 使用js实现思维导图
使用js实现思维导图 demo:http://rockyren.github.io/mindmaptree/ 源码:http://github.com/RockyRen/mindmaptree/tre ...
- HTML DOM 属性
innerHTML 属性 获取元素内容的最简单方法是使用 innerHTML 属性. innerHTML 属性对于获取或替换 HTML 元素的内容很有用. 实例 下面的代码获取 id="in ...
- vscode 创建.net core mvc
cd 进一个文件夹 1,创建一个sln 工程文件 [ dotnet new sln -n Demo1 ] 2,创建一个mvc项目 [ dotnet new mvc -n Demo1.Web ] 3, ...