1 Star 0 Fork 63

雁秋 / C-Sharp

forked from 编程语言算法集 / C-Sharp 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
BurrowsWheelerTransform.cs 2.32 KB
一键复制 编辑 原始数据 按行查看 历史
Andrii Siriak 提交于 2021-05-08 21:42 . Cleanup minor things (#221)
using System;
using System.Linq;
namespace Algorithms.DataCompression
{
/// <summary>
/// The Burrows–Wheeler transform (BWT) rearranges a character string into runs of similar characters.
/// This is useful for compression, since it tends to be easy to compress a string that has runs of repeated
/// characters.
/// See <a href="https://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform">here</a> for more info.
/// </summary>
public class BurrowsWheelerTransform
{
/// <summary>
/// Encodes the input string using BWT and returns encoded string and the index of original string in the sorted
/// rotation matrix.
/// </summary>
/// <param name="s">Input string.</param>
public (string encoded, int index) Encode(string s)
{
if (s.Length == 0)
{
return (string.Empty, 0);
}
var rotations = GetRotations(s);
Array.Sort(rotations, StringComparer.Ordinal);
var lastColumn = rotations
.Select(x => x[^1])
.ToArray();
var encoded = new string(lastColumn);
return (encoded, Array.IndexOf(rotations, s));
}
/// <summary>
/// Decodes the input string and returns original string.
/// </summary>
/// <param name="s">Encoded string.</param>
/// <param name="index">Index of original string in the sorted rotation matrix.</param>
public string Decode(string s, int index)
{
if (s.Length == 0)
{
return string.Empty;
}
var rotations = new string[s.Length];
for (var i = 0; i < s.Length; i++)
{
for (var j = 0; j < s.Length; j++)
{
rotations[j] = s[j] + rotations[j];
}
Array.Sort(rotations, StringComparer.Ordinal);
}
return rotations[index];
}
private string[] GetRotations(string s)
{
var result = new string[s.Length];
for (var i = 0; i < s.Length; i++)
{
result[i] = s.Substring(i) + s.Substring(0, i);
}
return result;
}
}
}
C#
1
https://gitee.com/xyesterday/C-Sharp.git
git@gitee.com:xyesterday/C-Sharp.git
xyesterday
C-Sharp
C-Sharp
master

搜索帮助