diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..e7e7cb3 --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,6 @@ +image: Visual Studio 2019 + +build_script: + - For /R %%I in (*.sln) do dotnet test %%I + +test: off \ No newline at end of file diff --git a/hw1BWT/hw1BWT.sln b/hw1BWT/hw1BWT.sln new file mode 100644 index 0000000..bd7f4a5 --- /dev/null +++ b/hw1BWT/hw1BWT.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31005.135 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hw1BWT", "hw1BWT\hw1BWT.csproj", "{0C32A11B-52F3-43CB-902C-90E3FCC9BA8E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0C32A11B-52F3-43CB-902C-90E3FCC9BA8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0C32A11B-52F3-43CB-902C-90E3FCC9BA8E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0C32A11B-52F3-43CB-902C-90E3FCC9BA8E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0C32A11B-52F3-43CB-902C-90E3FCC9BA8E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {921A791C-3E9C-4F52-8699-DB40DAFD590A} + EndGlobalSection +EndGlobal diff --git a/hw1BWT/hw1BWT/BWT.cs b/hw1BWT/hw1BWT/BWT.cs new file mode 100644 index 0000000..c5893a3 --- /dev/null +++ b/hw1BWT/hw1BWT/BWT.cs @@ -0,0 +1,124 @@ +using System; +using System.Linq; + +namespace hw1BWT +{ + static class BWT + { + public static bool Test() + { + string string1 = "abcab"; + var (result, numberOfString) = BWTtransform(string1); + if (result != "cbaab") + { + return false; + } + string oldString1 = ReverseBWT("cbaab", numberOfString); + return oldString1 == string1; + } + + private static int[] WriteSuffixArray(string str) + { + var suffixArray = new int[str.Length]; + for (int i = 0; i < str.Length; i++) + { + suffixArray[i] = i; + } + return suffixArray; + } + + public static Tuple BWTtransform(string str) + { + var suffixArray = WriteSuffixArray(str); + for (int i = 1; i < suffixArray.Length; i++) + { + for (int j = 0; j < suffixArray.Length - i; j++) + { + string str1 = str.Substring(suffixArray[j]) + str.Substring(0, suffixArray[j]); + string str2 = str.Substring(suffixArray[j + 1]) + str.Substring(0, suffixArray[j + 1]); + if (String.Compare(str1, str2) > 0) + { + int swaper = suffixArray[j]; + suffixArray[j] = suffixArray[j + 1]; + suffixArray[j + 1] = swaper; + } + } + } + string result = ""; + var strSize = str.Length; + for (int i = 0; i < suffixArray.Length; i++) + { + if (suffixArray[i] == 0) + { + result += str[strSize - 1]; + } + else + { + result += str[suffixArray[i] - 1]; + } + } + return new Tuple(result, Array.IndexOf(suffixArray, 0)); + } + + private static char[] GetAlphabet(char[] str) + { + string alphabet = ""; + for (int i = 0; i < str.Length; ++i) + { + if (!alphabet.Contains(str[i])) + { + alphabet += str[i]; + } + } + char[] alphabetArray = alphabet.ToCharArray(0, alphabet.Length); + Array.Sort(alphabetArray); + return alphabetArray; + } + + public static string ReverseBWT(string str, int numberOfStr) + { + char[] line = str.ToCharArray(0, str.Length); + char[] alphabetOfString = GetAlphabet(line); + var countSymbolsInStr = new int[alphabetOfString.Length]; + for (int i = 0; i < alphabetOfString.Length; ++i) + { + countSymbolsInStr[i] = line.Where(x => x == alphabetOfString[i]).Count(); + } + var numerationSymbols = new int[alphabetOfString.Length]; + int sumSymbols = 0; + for (int i = 0; i < countSymbolsInStr.Length; ++i) + { + sumSymbols += countSymbolsInStr[i]; + if (i == 0) + { + numerationSymbols[i] = 0; + } + else + { + numerationSymbols[i] = sumSymbols - countSymbolsInStr[i]; + } + } + var helper = new int[line.Length]; + for (int i = 0; i < line.Length; i++) + { + for (int j = 0; j < alphabetOfString.Length; j++) + { + if (line[i] == alphabetOfString[j]) + { + helper[i] = numerationSymbols[j]; + numerationSymbols[j]++; + } + } + } + var answer = new char[line.Length]; + for (int i = line.Length - 1; i > -1; i--) + { + answer[i] = line[numberOfStr]; + numberOfStr = helper[numberOfStr]; + } + var result = new String(answer); + result = result.Substring(0, result.Length); + return result; + } + } +} \ No newline at end of file diff --git a/hw1BWT/hw1BWT/Program.cs b/hw1BWT/hw1BWT/Program.cs new file mode 100644 index 0000000..35024f6 --- /dev/null +++ b/hw1BWT/hw1BWT/Program.cs @@ -0,0 +1,24 @@ +using System; +using System.Linq; + +namespace hw1BWT +{ + class Program + { + static void Main() + { + if (!BWT.Test()) + { + Console.WriteLine("Тест не пройден!"); + return; + } + Console.WriteLine("Тест пройден!"); + Console.WriteLine("Введите строку: "); + string line = Console.ReadLine(); + var (result, numberOfString) = BWT.BWTtransform(line); + Console.WriteLine("Строка после алгоритма: " + result); + string res2 = BWT.ReverseBWT(result, numberOfString); + Console.WriteLine("Исходная строка: " + res2); + } + } +} \ No newline at end of file diff --git a/hw1BWT/hw1BWT/hw1BWT.csproj b/hw1BWT/hw1BWT/hw1BWT.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/hw1BWT/hw1BWT/hw1BWT.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + +