HDU-6740-J. MUV LUV EXTRA

HDU-6740-J. MUV LUV EXTRA

题目

MUV LUV EXTRA
Time Limit: 2000/1500 MS (Java/Others)
Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 520 Accepted Submission(s): 159

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

题目大意

给你一个小数让你求循环节的,但是要求循环节的可信度最高,可信度由 a×循环节已经开始出现的部分长度−b×循环节长度;

解题思路

我们可以先将小数点的字符串倒过来,然后利用kmp来求循环周期,这样最后从后向前依次遍历就行了,这个题很巧妙的利用了 KMP

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int ms=1e7+9; 
char s[ms],c[ms]; 
int ne[ms];
void shuzu(int x)
{
// 利用 KMP 求循环节	
	ne[0]=-1;
	int j=-1;
	int i=0;
	while(i<x)
	{
		if(j==-1||c[i]==c[j])
		{
			i++,j++,ne[i]=j;
		}
		else j=ne[j];
	}
}

int main()
{
	ll n,m;
	while(~scanf("%lld%lld%s",&n,&m,s))
	{
		int len=strlen(s);
		int k=0;
		for(int i=len-1;i>0&&s[i]!='.';i--)
		{
			c[k++]=s[i];
		}
		c[k]='#';
		shuzu(len);
		ll maxn=-0x3f3f3f3f3f3f3f3f;
// 遍历求最大值		
		for(int i=0;i<k;i++)
		{
			maxn=max(maxn,n*(i+1)-m*(i+1-ne[i+1]));	
		}
		printf("%lld\n",maxn);	
	}
	return 0;
}
发布了49 篇原创文章 · 获赞 14 · 访问量 4359

猜你喜欢

转载自blog.csdn.net/qq_43750980/article/details/101701105
今日推荐