励志用少的代码做高效表达


Problem description

One day, Kagami Sumika is stuck in a math problem aiming at calculating the length of a line segment with given statements and constraints. Since Sumika has no idea about it, she takes out a ruler and starts to measure the length. Unfortunately, the answer is an infinite decimal and she only got the first some digits of the answer from the ruler.
Sumika guesses that the answer is a rational number, which means that there exists two integers p, q that the answer equals qp. In this situation, the answer can be expressed as an infinte repeated decimal. For example, 12 = 0.500 … , 13 = 0.333 … , 910= 0.8999 … ,3635= 1.0285714285714 … .Sumika wants to guess the original number from the digits she got. Note that a number may has more than one way to be expressed such as 1.000 … = 0.999 … . Sumika won’t transform the digits she got to another form when guessing the original number.
Furthermore, Sumika relizes that for a repeating part, either too long or the appeared length too short will make the result unreliable. For example, if the decimal she measured is 1.0285714285714, it is obviously unreliable that the repeating part is “0285714285714”, since it is too long, or “428571”, since the appeared length is too short, which equals 7, the length of “4285714”. In this case, the best guess is “285714”, whose length is 6 and the appeared length is 12. So formally, she defines the reliability value of a repeating part, whose length is l and the appeared length is p, as the following formula:
a∗p−b∗la * p - b * la∗p−b∗l
Where a and b are given parameters.
Last but not least, you can ignore the integer parts of the decimal. It is just for restoring the scene. And the repeating part you guess should be completely repeated at least once and is still repeating at the end currently.
Please help Sumika determine the maximum reliability value among all repeating parts.

Input

The first line contains two positive integers a, b (1 ≤ a, b ≤ 109), denoting the parameters.
The next line contains a string s (1 ≤ |s| ≤ 107) in decimal form, denoting the first some digits of the accurate result.
It is guaranteed that there is exactly one decimal point in s and s is a legal non-negative decimal without leading “-”(the minus sign).

Output

Output a single line containing an integer, denoting the maximum reliability value.


解题思路

如果有对KMP还不懂的同学——>KMP算法_图示分析+解析+例题

这个题很巧妙的利用了 KMP

我们需要反过来求kmp得next数组,枚举前缀,那么i就是循环节出现的总长度,而i-next[i]就是循环节长度。


代码展示

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF=1e18;
ll a,b,res;
int i,j,k;
char s[10000010], s1[10000010];
int Next[10000010];void getnext(char s[],int Next[]) {int q,k;int len=strlen(s);Next[0] = 0;for (q = 1,k = 0; q < len; ++q) {while(k>0 && s[q]!=s[k]) k = Next[k-1];if (s[q] == s[k]) k++;Next[q] = k;}
}int main() {while(cin>>a>>b) {memset(Next,0,sizeof Next);cin>>s1;int len1=strlen(s1);int len=0;for(i=len1-1; i>=0; i--) {if(s1[i]=='.') break;s[len1-i-1]=s1[i];len++;}getnext(s,Next);ll ans=-INF;for(i=0; i<len; i++) {res=a*(i+1)-b*(i+1-Next[i]);    //核心代码if(res>ans) ans=res;}cout << ans << '\n';}
return 0; }

39行代码AC_HDU-6740 2019CCPC秦皇岛 J MUV LUV EXTRA(KMP变形)相关推荐

  1. 2019ccpc秦皇岛J MUV LUV EXTRA(KMP扩展)

    Problem Description 鉴纯夏是一名成绩不太好的高中生.一天她在数学考试中碰到了一道求某条线段长度的问题.因为她并不会做这道题,所以她准确地作图后用尺子量出了这条线段的长度.不幸的是, ...

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

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

  3. J - MUV LUV EXTRA

    J - MUV LUV EXTRA 思路: k m p kmp kmp. 求小数点后面的翻转字符串的循环节和循环长度. 注意循环节长度为1时,答案是 a − b a-b a−b. 时间复杂度: O ( ...

  4. 2019 CCPC 秦皇岛: MUV LUV EXTRA

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

  5. 2019CCPC秦皇岛 K MUV LUV UNLIMITED(思维博弈)

    2019CCPC秦皇岛K 这个题感觉就是头脑风暴吧,关键在于抓住正确的方向想下去,我中间也跑偏了几次... 定义一个分支为从叶子往根的方向,不存在包含多个子节点的节点序列,即从叶子到包含多个子节点的节 ...

  6. 2019CCPC秦皇岛 K MUV LUV UNLIMITED(博弈)

    MUV LUV UNLIMITED Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  7. 2019秦皇岛CCPC J - MUV LUV EXTRA

    题意:从一个小数的小数部分,计算下面式子的最大值 其中a和b是给定的参数,p是循环结已知的长度,l是循环结的长度 例如对于abab,p是4,l就是2 思路: 贪心的来说我们要让p更大,l尽可能小,所以 ...

  8. J - MUV LUV EXTRA(KMP)

    感受 思 路 很 简 单 , 就 是 不 知 道 k m p 还 可 以 求 最 小 循 环 节 长 度 思路很简单,就是不知道kmp还可以求最小循环节长度 思路很简单,就是不知道kmp还可以求最小循 ...

  9. 2019 CCPC 秦皇岛 MUV LUV EXTRA kmp

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

最新文章

  1. python 遍历字典
  2. AngularJS』一点小小的理解
  3. Hibernate---O/R Mapping
  4. jQuery.sap.declare(cus.crm.notes.ext.Component);
  5. mysql5.7 only_full_group_by_Mysql5.7及以上版本 ONLY_FULL_GROUP_BY报错的解决方法
  6. 如何用texstudio下载ctex_公众号素材库视频如何下载,用这种方法就可以哦
  7. LeetCode 390. 消除游戏(类似约瑟夫环,找映射规律)
  8. List集合和set集合
  9. visual studio快捷键总结
  10. 中国雅虎首页改版彻底与口碑网剥离
  11. 多数据源配置MyBatisPlus(十八)
  12. Linux优化学习之Load Average (平均负载)
  13. 微信小程序 -- 语音合成:将文字转为语音(插件:微信同声传译)
  14. 微信开放平台开发-授权、全网发布(PHP)
  15. E. The Humanoid Codeforces Round #834 (Div. 3)(暴力dfs?)
  16. matlab具有复数,MATLAB:具有复数的printmat
  17. 昨夜西风凋碧树,独上高楼,望尽天涯路
  18. 科学家发现病毒感染细胞和染色体密切相关,提取出这些染色体的最大相似之处
  19. DENY与REVOKE的区别
  20. usb万能驱动win7_8代能不能装win7?测给你看

热门文章

  1. 关于JUnit5 你必须知道的(一) JUnit5架构和环境搭建
  2. 如何用ELK搭建TB级的日志监控系统?
  3. Redis简介及安装
  4. Python中的select、epoll详解
  5. Intel和AMD的最新视频编码/解码基准测试
  6. 技术沙龙直播 | 数据库技术探索及行业应用
  7. win下安装elasticsearch(win_Elasticsearch)
  8. 左神算法:分别用递归和非递归方式实现二叉树先序、中序和后序遍历(Java版)
  9. 【Java】Java实现 JSON 的组装和解析
  10. 【Java爬虫】我的第一个爬虫 -- 简单抓取网页源代码