【USACO 3.1.5】联系

【描述】

奶牛们开始对用射电望远镜扫描牧场外的宇宙感兴趣。最近,他们注意到了一种非常奇怪的脉冲调制微波从星系的中央发射出来。他们希望知道电波是否是被某些地外生命发射出来的,还是仅仅是普通的的星星发出的。

帮助奶牛们用一个能够分析他们在文件中记下的记录的工具来找到真相。他们在寻找长度在A到B之间(含)在每天的数据文件中重复得最多的比特序列 (1 <= A <= B <= 12)。他们在找那些重复得最多的比特序列。一个输入限制告诉你应输出多少频率最多的序列。

符合的序列可能会重叠,并且至少出现一次的序列会被计数。

【格式】

PROGRAM NAME: contact

INPUT FORMAT:

(file contact.in)

第一行: 三个用空格分隔的整数: A, B, N; (1 <= N < 50)

第二行及以后: 一个最多200,000字符的序列,全是0或1; 每行字符数不大于80。

OUTPUT FORMAT:

(file contact.out)

输出N个频率最高的序列(按照频率由高到低的次序)。由短到长排列频率相同的这些序列,如果长短相同,按二进制大小排列。如果出现的序列个数小于N,输出存在的序列。

对于每个存在的频率,先输出单独包含该频率的一行,再输出以空格分隔的这些序列。每行六个(除非少于六个剩下)。

【分析】

直接上字典树。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cmath>
  4 #include <cstring>
  5 #include <algorithm>
  6 const int maxnode=200000;
  7 const int maxl=200000+10;
  8 const int maxn=5000+10;
  9 using namespace std;
 10 struct Tire
 11 {
 12        int ch[maxnode][3];
 13        int val[maxnode],sz,father[maxnode];
 14        Tire(){memset(ch[0],0,sizeof(ch[0]));val[0]=0;sz=1;father[0]=-1;}
 15        void insert(char *t,int from,int to)
 16        {
 17             int i,u=0;
 18             for (i=from;i<=to;i++)
 19             {
 20                 int c=(t[i]=='1'?1:0);
 21                 if (!ch[u][c])//没有建立过该节点
 22                 {
 23                     memset(ch[sz],0,sizeof(ch[sz]));
 24                     val[sz]=0;
 25                     father[sz]=u;
 26                     ch[u][c]=sz++;
 27                 } 
 28                 u=ch[u][c];
 29             }
 30             val[u]++;//累加 
 31        }
 32 }tire;
 33 bool check(int a,int b)
 34 {
 35      char A[15],B[15];
 36      int pa=0,pb=0;
 37      while (tire.father[a]!=-1)
 38      {
 39            int u=tire.father[a];
 40            if (tire.ch[u][0]==a) A[pa++]=0;else A[pa++]=1;
 41            a=tire.father[a];
 42      }
 43      while (tire.father[b]!=-1)
 44      {
 45            int u=tire.father[b];
 46            if (tire.ch[u][0]==b) B[pb++]=0;else B[pb++]=1;
 47            b=tire.father[b];
 48      }
 49      if (pa>pb) return 0;else if (pb>pa)return 1;
 50      for (int i=pa-1;i>=0;i--) if (A[i]>B[i]) return 0;else if (B[i]>A[i]) return 1;
 51 }
 52 struct Ans
 53 {
 54        int num;//节点编号 
 55        int cnt;//统计数量
 56        bool operator < (const Ans&b)const
 57        {
 58             if (cnt==b.cnt) return check(num,b.num);
 59             return cnt>b.cnt;
 60        } 
 61 }ans[maxn];
 62 int f=0;
 63 char str[maxl],temp;
 64 void print(int num,int flag);
 65 int main()
 66 {
 67     int len=0,a,b,n,i,j,k,ans_point=0;
 68     int n1=0;//用来统计已经输出过的频率; 
 69     //文件操作
 70     freopen("contact.in","r",stdin);
 71     freopen("contact.out","w",stdout);
 72     scanf("%d%d%d",&a,&b,&n);
 73     while (scanf("%c",&temp)!=EOF) if (temp=='1' || temp=='0') str[len++]=temp;
 74 
 75     for (i=0;i<len;i++)
 76     {
 77         if (i+a-1>=len) break;
 78         for (j=i+a-1;j<=min(i+b-1,len-1);j++) tire.insert(str,i,j);//插入 
 79     }
 80     for (i=0;i<tire.sz;i++) if (tire.val[i]!=0) {ans[ans_point].num=i;ans[ans_point++].cnt=tire.val[i];}
 81     sort(ans,ans+ans_point);
 82     //for (i=0;i<ans_point;i++) printf("%d\n",ans[i].cnt);
 83     f=0;
 84     for (i=0;;i++)
 85     {
 86         if (i==ans_point) break;
 87         if (i==0 || ans[i].cnt!=ans[i-1].cnt) 
 88         {
 89                  n1++;
 90                  if (n1>n) break; 
 91                  print(ans[i].num,1);
 92         }//打印 
 93         else print(ans[i].num,0);
 94         f=1;
 95     }
 96     return 0;
 97 }
 98 void print(int num,int flag)
 99 {
100      int t=0,i;
101      if (flag==1 && f==1) printf("\n%d\n",tire.val[num]);
102      else if (flag==1 && f==0) printf("%d\n",tire.val[num]);
103      char temp[15];
104      while (tire.father[num]!=-1)
105      {
106            int u=tire.father[num];
107            if (tire.ch[u][0]==num) temp[t++]=0;
108            else temp[t++]=1;
109            num=tire.father[num];
110      }
111      for (i=t-1;i>=0;i--) printf("%c",char(temp[i]+'0'));
112      printf(" ");
113 }

 

posted @ 2014-06-24 22:22  TCtower  阅读(302)  评论(0编辑  收藏  举报