poj 3464(Trie)Approximations

Posted hk lin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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


 

董华星在他09年的论文里说可以用字典树写。。我试着写了下,然而感觉题目是不是给的范围有问题啊。测了好多样例没问题。欢迎各位大佬给个样例测测0 0。

代码如下:

技术分享图片
  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 {
 20     int now=root,p;
 21     int len=strlen(s);
 22     for(int i=0;i<len;i++)
 23     {
 24         p=s[i]-0;
 25         if(next[now][p]==0)
 26         {
 27             next[now][p]=++ttot;
 28         }
 29         now=next[now][p];
 30         num[now]++;
 31     }
 32     return ;
 33 }
 34 int ans,prenode;
 35 void dfs(int now,int prenum)
 36 {
 37     int nownum,p;
 38     for(int i=0;i<10;i++)
 39     {
 40         nownum=prenum;
 41         p=next[now][i];
 42         if(i==0)
 43         {
 44             if(prenode!=0)
 45             {
 46                 prenode=next[prenode][9];
 47                 if(prenode!=0)
 48                 {
 49                     for(int j=5;j<10;j++)
 50                         if(next[prenode][j]!=0)
 51                             nownum+=num[next[prenode][j]];
 52                 }
 53             }
 54         }
 55         else
 56         {
 57             if(next[now][i-1]!=0)
 58                for(int j=5;j<10;j++)
 59                 {
 60                     if(next[next[now][i-1]][j]!=0)
 61                     {
 62                         nownum+=num[next[next[now][i-1]][j]];
 63                         if(i-1>=5)
 64                         {
 65                             nownum+=num[next[next[now][i-1]][j]];
 66                         }
 67                     }
 68 
 69                 }
 70         }
 71         if(i!=0)
 72         {
 73             prenode=next[now][i-1];
 74         }
 75         if(p!=0)
 76         {
 77             nownum+=num[p];
 78             for(int j=5;j<10;j++)
 79             {
 80                 if(next[p][j]!=0)
 81                 {
 82                     nownum-=num[next[p][j]];
 83                 }
 84             }
 85             if(i>=5)
 86                 nownum+=num[p];
 87             if(nownum>ans)
 88                 ans=nownum;
 89 //            cout<<i<<" "<<nownum<<endl;
 90             dfs(p,nownum);
 91         }
 92         else
 93         {
 94             if(nownum>ans)
 95                 ans=nownum;
 96         }
 97     }
 98     return ;
 99 }
100 int main()
101 {
102     scanf("%d",&n);
103     ttot=root=0;
104     for(int i=1;i<=n;i++)
105     {
106         scanf("%s",s);
107         tadd(s+2,root);
108     }
109     ans=0;
110     dfs(root,0);
111     printf("%d\n",ans);
112     return 0;
113 }
View Code

 


以上是关于poj 3464(Trie)Approximations的主要内容,如果未能解决你的问题,请参考以下文章

poj 2513 欧拉图/trie

poj3630 Phone List (trie树模板题)

Trie树模板 POJ1056

poj 2513 并查集,Trie(字典树), 欧拉路径

Trie入门--Poj3630 Phone List

POJ 2001 Shortest Prefixes (Trie)