MUV LUV EXTRA

Time Limit: 2000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 134    Accepted Submission(s): 49

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 * 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.
 
Sample Input
5 3
1.1020
2 1
12.1212
 
Sample Output
9
6
 
Source
 

题解:

是个套了外壳的字符串题,求得是后缀的循环节长度和循环节出现的总长度(包括不完整循环节),显然我们反过来求kmp得next数组,枚举前缀,那么i就是循环节出现的总长度,而i-next[i]就是循环节长度。

参考代码:

#include<bits/stdc++.h>
#define maxl 10000010
using namespace std; const long long inf=1ll<<;
long long a,b,ans;
int slen,tlen;
int nxt[maxl];
char s[maxl],t[maxl]; inline void mainwork()
{
ans=a-b;
int j=;nxt[]=;
for(int i=;i<=tlen;i++)
{
while((j && t[j+]!=t[i]) || (j==tlen))
j=nxt[j];
if(t[j+]==t[i] && j+<=tlen)
j++;
nxt[i]=j;ans=max(ans,a*i-b*(i-nxt[i]));
}
} int main()
{
while(~scanf("%lld%lld",&a,&b))
{
scanf("%s",s+);
slen=strlen(s+);tlen=;
for(int i=slen;i>=;i--)
if(s[i]=='.')
break;
else
t[++tlen]=s[i];
mainwork();
printf("%lld\n",ans);
}
return ;
}

最新文章

  1. 巧用 mask-image 实现简单进度加载界面
  2. 后进先出 stack、 先进先出Queue
  3. [Android Tips] 6. Parallax ViewPager
  4. JavaScript Array(数组)对象
  5. 第52课 C++中的抽象类和接口
  6. Perl 语法 - 高级特性
  7. 如何在PL/SQL Developer 中设置 在select时 显示所有的数据
  8. Java简介(4)-关键字
  9. 数据结构(C语言版)顺序栈相关算法的代码实现
  10. HTTP协议中PUT和POST使用区别
  11. Android学习第十天
  12. Golang的日志处理
  13. COGS 2392 2393 2395 有标号的二分图计数
  14. 第二十三篇-ubuntu18.04怎么下载播放器以及如何设置默认播放器
  15. mysql登录报错:ERROR 1045 (28000): Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password: YES)
  16. 怎样找回被删除的EXCEL
  17. js关于移入移出延迟提示框效果处理
  18. 洛谷 P2245 星际导航 解题报告
  19. 【转】我的第一次和最后一次 Hackathon 经历
  20. css3半圆

热门文章

  1. python中字典的基础操作
  2. 使用 vue-element-admin 动态路由渲染
  3. (二十六)golang--切片
  4. requests模拟登陆的三种方式
  5. React-基础总结
  6. 在Raspberry Pi上创建容器
  7. git 删除误上传的.idea文件
  8. 新闻实时分析系统 Spark2.X环境准备、编译部署及运行
  9. JAVA _____Scanner用法
  10. NTP服务编译安装报错:ntpd.c:124:29: 致命错误:sys/capability.h:没有那个文件或目录