MUV LUV EXTRA

[Link](Problem - J - Codeforces)

题意

给你a和b和一个小数,让你找 a × 循 环 节 出 现 到 结 尾 的 长 度 + b × 循 环 节 长 度 a\times循环节出现到结尾的长度+b\times循环节长度 a×循环节出现到结尾的长度+b×循环节长度的最大值是多少?

题解

把小数部分倒过来,等价于找当前这个字符串的循环节是多少。这个可以用kmp中的ne数组解决,长度为len的字符的循环节就是 l e n − n e [ l e n ] len-ne[len] len−ne[len]。知道这个性质以后直接求出ne数组,暴力枚举一遍即可。

循环节的证明[link](kmp next函数 kmp的周期问题,深入了解kmp中next的原理 - Because Of You - 博客园 (cnblogs.com))

Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath>
#include <stack>
#include <iomanip>
#include <deque>
#include <sstream>
#define x first
#define y second
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e7 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
LL n, m, k;
int ne[N];
char str1[N], str2[N];
int main() {ios::sync_with_stdio(false), cin.tie(0);cin >> n >> m;cin >> str1;int len = strlen(str1);int cnt = 0;for (int i = len - 1; i >= 0; i -- )    if (str1[i] == '.') break;else str2[++ cnt] = str1[i];LL res = -INF;ne[0] = ne[1] = 0;for (int i = 2; i <= cnt; i ++ ) {int j = ne[i - 1];while (j && str2[i] != str2[j + 1]) j = ne[j];if (str2[j + 1] == str2[i]) j ++;ne[i] = j;}for (int i = 1; i <= cnt; i ++ ) res = max(res, n * i - m * (i - ne[i]));cout << res << endl;return 0;
}

MUV LUV EXTRA(Kmp找最小循环节)相关推荐

  1. Problem A. MUV LUV EXTRA(kmp求最小循环节)

    LINK 最小化 a ∗ p − b ∗ l a*p-b*l a∗p−b∗l 其中 p p p表示循环节总长, l l l表示循环节长度 若循环节从 i i i位置开始,那么一定需要延续到末尾 考虑固 ...

  2. J. MUV LUV EXTRA (KMP求最小循环节)

    原题链接 枚举后缀,KMP求每种后缀的最小循环节,更新最优解. #include <bits/stdc++.h> #define int long long using namespace ...

  3. 【HDU - 3746 】Cyclic Nacklace (KMP,最小循环节问题)

    题干: CC always becomes very depressed at the end of this month, he has checked his credit card yester ...

  4. kmp求最小循环节及最小循环周期

    只列出求最小循环节部分 for(int i=1;i<=n;i++){int temp=i-next[i];if(i%temp==0 && i/temp>1)printf(& ...

  5. hdu6740 MUV LUV EXTRA KMP算法

    题解:反向用next数组求循环节(前i个字符的)即可. 题目:https://vjudge.net/problem/HDU-6740 #include<bits/stdc++.h> #de ...

  6. 2019 CCPC 秦皇岛 MUV LUV EXTRA kmp

    传送门 枚举循环节已出现的长度 p,最优的循环节就是最后 p 个字 符构成的字符串的最短周期. 考虑把字符串倒过来,使用 kmp 可以求出每个前缀的最短 周期,即求出了原串每个后缀的最短周期. #in ...

  7. 2019 CCPC 秦皇岛: MUV LUV EXTRA

    MUV LUV EXTRA (本篇主要内容,kmp求最短循环节) 题目传送门: MUV LUV EXTRA 题意: 给你一个字符串和两个整数a和b.在小数点后,找到一个循环节 l,循环长度为p.求 a ...

  8. 2019 CCPC秦皇岛 J 题 MUV LUV EXTRA【KMP 求最小循环节】

    题意: 原题意是给出一个有理数的前一部分,求这个有理数的无限循环部分是什么.有一个值来评估猜的准确度.转换一下就成了下面的题意: 给出一个字符串s,有某一个子串,设p为该子串在s的某个后缀中的匹配长度 ...

  9. 2019CCPC秦皇岛赛区 hdu 6740 MUV LUV EXTRA(KMP next数组)

    传送门 MUV LUV EXTRA Time Limit: 2000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Othe ...

最新文章

  1. 损失函数(Loss Function) -1
  2. ldconfig及LD_LIBRARY_PATH
  3. 从Java新手到大神需要学哪些知识?
  4. ios html下拉刷新,Lottie_ios 实现下拉刷新
  5. Http协议:状态码
  6. js中怎么写自执行函数
  7. SAP License:ERP具有的九种优势
  8. linux思源黑体乱码,deepin终端使用思源黑体做中文字体
  9. 一个珊瑚虫倒下了,千万个珊瑚虫站起来!欢呼吧QQ****下载,不断更新
  10. 主成分分析(PCA)详解
  11. 一文搞懂this指向
  12. 饮料自动售货机模拟(小项目)
  13. USACO 2018 February Contest, Silver-Rest Stops
  14. 求最大公约数,最小公倍数
  15. 记忆日语的奥秘—日语汉字读音变化
  16. Servlet的工作原理简介
  17. Win10 LTSC 2021的BUG处理
  18. AGS 对象池和状态概要
  19. 年度总结(依然仰望星空,知世俗不世俗)
  20. AES-128加密解密方式(逐步更新)

热门文章

  1. 现在当兵有什么待遇复原以后_当兵退伍后的各种待遇
  2. [数据结构](线性表之单链表)
  3. fastadmin导出excel数字格式,支持excel求和运算
  4. 怎么实现角色权限的分配_新生日记2“铁血妈妈”刘璇和“艺术慈父”王弢,你家角色怎么分配?...
  5. 怎么通过DELL服务器的iDrac口查看硬件信息 192.168.0.120 root calvin
  6. stomp java_Java 实现Stomp WebSocket 接收
  7. 阿里域名、bluehost建站
  8. python装饰器class_python装饰器2:类装饰器
  9. Apollo(阿波罗)分布式配置安装详解
  10. 安装BurpSuite