poj 3464(Trie)Approximations

实践是知识的母亲,知识是生活的明灯。这篇文章主要讲述poj 3464(Trie)Approximations相关的知识,希望能为你提供帮助。
Approximations

Time Limit:  2000MS   Memory Limit:  131072K
Total Submissions:  419   Accepted:  23
Description
For any decimal fraction, we can obtain a set of approximations of different accuracy by mean of rounding. Take 0.2503 for example, we have the following approximations:
  • 0.2503
  • 0.250
  • 0.25
  • 0.3
  • 0.
If two fractions  A  and  B  can both be rounded to  C, we call  C  a  common approximation  of  A  and  B. Two fractions may have more than one common approximations, each having a distinct accuracy. For example, 0.2503 and 0.2504 have common approximations 0.250 and 0.25. The accuracy of the former is 10?3, while that of the latter is 10?2. Among all common approximations of two fractions, there is one that has the highest accuracy, and we call it the  most accurate common approximation  (MACA) of the two fractions. By this definition, the MACA of 0.2503 and 0.2504 is 0.250.
Given  N  fractions  Ai  (1 ≤  i  ≤  N) in the range [0, 0.5), find a fraction  x  that maximizes the sum of ?log10  (the accuracy of the MACA of  Ai  and  x). Report that maximized sum.
Input
The first line contains one integer  NN  ≤ 100000.
Each of the next  N  lines contains a decimal fraction  Ai. The total number of digits of the  N  decimal fractions doesn‘t exceed 400000. There is always a radix point, so zero is "0." instead of "0".
Output
One integer, the maximized sum.
Sample Input
4 0.250 0.2506 0.25115 0.2597

Sample Output
11

Hint
x  = 0.25115.Source
POJ Monthly--2007.11.25, Yang Yi  董华星在他09年的论文里说可以用字典树写。。我试着写了下,然而感觉题目是不是给的范围有问题啊。测了好多样例没问题。欢迎各位大佬给个样例测测0 0。
代码如下:
poj 3464(Trie)Approximations

文章图片
poj 3464(Trie)Approximations

文章图片
1 #include< cstdio> 2 #include< iostream> 3 #include< cstring> 4 #define clr(x) memset(x,0,sizeof(x)) 5 #define clr_1(x) memset(x,-1,sizeof(x)) 6 #define LL long long 7 #define mod 1000000007 8 #define INF 0x3f3f3f3f 9 #define next nexted 10 using namespace std; 11 const int N=1e5+10; 12 const int M=4e5+10; 13 int next[M][10]; 14 int num[M]; 15 char s[M]; 16 int n,m,k; 17 int root,ttot; 18 void tadd(char *s,int root) 19 { 20int now=root,p; 21int len=strlen(s); 22for(int i=0; i< len; i++) 23{ 24p=s[i]-‘0‘; 25if(next[now][p]==0) 26{ 27next[now][p]=++ttot; 28} 29now=next[now][p]; 30num[now]++; 31} 32return ; 33 } 34 int ans,prenode; 35 void dfs(int now,int prenum) 36 { 37int nownum,p; 38for(int i=0; i< 10; i++) 39{ 40nownum=prenum; 41p=next[now][i]; 42if(i==0) 43{ 44if(prenode!=0) 45{ 46prenode=next[prenode][9]; 47if(prenode!=0) 48{ 49for(int j=5; j< 10; j++) 50if(next[prenode][j]!=0) 51nownum+=num[next[prenode][j]]; 52} 53} 54} 55else 56{ 57if(next[now][i-1]!=0) 58for(int j=5; j< 10; j++) 59{ 60if(next[next[now][i-1]][j]!=0) 61{ 62nownum+=num[next[next[now][i-1]][j]]; 63if(i-1> =5) 64{ 65nownum+=num[next[next[now][i-1]][j]]; 66} 67} 68 69} 70} 71if(i!=0) 72{ 73prenode=next[now][i-1]; 74} 75if(p!=0) 76{ 77nownum+=num[p]; 78for(int j=5; j< 10; j++) 79{ 80if(next[p][j]!=0) 81{ 82nownum-=num[next[p][j]]; 83} 84} 85if(i> =5) 86nownum+=num[p]; 87if(nownum> ans) 88ans=nownum; 89 //cout< < i< < " "< < nownum< < endl; 90dfs(p,nownum); 91} 92else 93{ 94if(nownum> ans) 95ans=nownum; 96} 97} 98return ; 99 } 100 int main() 101 { 102scanf("%d",& n); 103ttot=root=0; 104for(int i=1; i< =n; i++) 105{ 106scanf("%s",s); 107tadd(s+2,root); 108} 109ans=0; 110dfs(root,0); 111printf("%d\n",ans); 112return 0; 113 }

View Code【poj 3464(Trie)Approximations】 


    推荐阅读