寻找重复序列

本文关键字:寻找 | 更新日期: 2023-09-27 18:29:58

我需要帮助找到合适的算法来解决我的目标。

假设我有一个数据集,里面有10000条关于一些事件的记录。我有50个事件类型,所以数据集中的每个记录都被分配了一个事件数(从1到50)。

我的数据集示例(2列:记录编号、事件编号):

1. 13
2. 24
3. 6
4. 50
5. 24
6. 6
...
10000. 46

正如你在这个例子中看到的,我有一个数字24,6的重复序列。现在我想知道我的数据集中有多少这样的序列以及其他未知序列。我也想知道每个序列的多重性。我已经检查了Rabin–Karp算法,但在我看来,我必须首先指定模式/序列。然而,我希望该算法能够自己找到它。

有人告诉我也要考虑分层集群,但我不确定它是否符合我的要求。

总之,我想找到一种算法,在上面的数据集中找到所有具有多重性的重复序列。

寻找重复序列

我假设您在一个与您提供的结构相同的文本文件中有这些数据,我使用LINQ对每个值进行分组和计数,如下代码所示:

static void Main(string[] args)
    {
        //read lines from the text file
        var arr = File.ReadAllLines("dataset.txt").AsQueryable();
        //convert the data to List<object> by convert each line to anonymous object
        var data = arr.Select(line => new { Index = line.Split('.')[0], Value = line.Split('.')[1] });
        //group the data by the value and then select the value and its count
        var res = data.GroupBy(item => item.Value).Select(group => new { Value = group.First().Value, Count = group.Count() });
        //printing result
        Console.WriteLine("Value't'tCount");
        foreach (var item in res)
        {
            Console.WriteLine("{0}'t't{1}",item.Value,item.Count);
        }
        Console.ReadLine();
    }

先前代码的结果

希望这对你有帮助。