Hiho 120 后缀数组一·重复旋律

来源:互联网 发布:艾丁格啤酒价格知乎 编辑:程序博客网 时间:2024/05/22 06:58

找连续K个中的最小值的最大值;根据Height数组的性质,若 rank[j] < rank[k],则后缀 Sj..n 和 Sk..n 的最长公共前缀为 min{height[rank[j]+1],height[rank[j]+2]...height[rank[k]]}。这个性质存在是因为我们对后缀按字典序排序。

所以连续K个的Height最小值代表出现至少为K次的旋律,再在里头找最大值,代表找出现次数至少为K次的最长旋律

using System;namespace Hiho{    class Program    {        static readonly int MAXN = 100005;        static char[] ch = new char[MAXN], All = new char[MAXN];        static int[] SA = new int[MAXN], rank = new int[MAXN], Height = new int[MAXN], tax = new int[MAXN], tp = new int[MAXN], a = new int[MAXN];        static int n, m;        //rank[i] 第i个后缀的排名; SA[i] 排名为i的后缀位置; Height[i] 排名为i的后缀与排名为(i-1)的后缀的LCP        //tax[i] 计数排序辅助数组; tp[i] rank的辅助数组(计数排序中的第二关键字),与SA意义一样。        //a为原串        static void RSort()        {            //rank第一关键字,tp第二关键字。            for (int i = 0; i <= m; i++) tax[i] = 0;            for (int i = 1; i <= n; i++) tax[rank[tp[i]]]++;            for (int i = 1; i <= m; i++) tax[i] += tax[i - 1];            for (int i = n; i >= 1; i--) SA[tax[rank[tp[i]]]--] = tp[i]; //确保满足第一关键字的同时,再满足第二关键字的要求        } //计数排序,把新的二元组排序。        static bool cmp(int[] f, int x, int y, int w) { return f[x] == f[y] && f[x + w] == f[y + w]; }        //通过二元组两个下标的比较,确定两个子串是否相同        static void Suffix(string str)        {            //init            char[] sArray = str.ToCharArray();            n = str.Length;            for (int i = 0; i < n; i++) a[i + 1] = sArray[i];            //SA            for (int i = 1; i <= n; i++)            {                rank[i] = a[i];                tp[i] = i;            }            m = 127; RSort(); //一开始是以单个字符为单位,所以(m = 127)            for (int w = 1, p = 1, i; p < n; w += w, m = p)            { //把子串长度翻倍,更新rank                //w 当前一个子串的长度; m 当前离散后的排名种类数                //当前的tp(第二关键字)可直接由上一次的SA的得到                for (p = 0, i = n - w + 1; i <= n; i++) tp[++p] = i; //长度越界,第二关键字为0                for (i = 1; i <= n; i++) if (SA[i] > w) tp[++p] = SA[i] - w;                //更新SA值,并用tp暂时存下上一轮的rank(用于cmp比较)                RSort();                int[] temp = rank;                rank = tp;                tp = temp;                rank[SA[1]] = p = 1;                //用已经完成的SA来更新与它互逆的rank,并离散rank                for (i = 2; i <= n; i++) rank[SA[i]] = cmp(tp, SA[i], SA[i - 1], w) ? p : ++p;            }            //离散:把相等的字符串的rank设为相同。            //LCP            int j, k = 0;            for (int i = 1; i <= n; Height[rank[i++]] = k)                for (k = k != 0 ? k - 1 : k, j = SA[rank[i] - 1]; a[i + k] == a[j + k]; ++k) ;            //这个知道原理后就比较好理解程序        }        static void Main(string[] args)        {            string[] lineArray = Console.ReadLine().Split(' ');            int N = int.Parse(lineArray[0]), K = int.Parse(lineArray[1]);            char[] array = new char[N];            for (int i = 0; i < N; ++i)            {                char ch = (char)Console.Read();                while (ch == '\r' || ch == '\n')                {                    ch = (char)Console.Read();                }                array[i] = ch;            }            Suffix(new string(array));            int ans = int.MinValue;            for (int i = 1; i <= n; ++i)            {                int tempH = int.MaxValue;                for (int j = 0; j < K - 1; ++j)                    if (Height[i + j] < tempH)                        tempH = Height[i + j];                if (tempH > ans)                    ans = tempH;            }            Console.WriteLine(ans);        }    }}
0 0
原创粉丝点击