From 273cf5002c49cd684664cd67bcce897c6e9b57f9 Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Fri, 5 Mar 2021 18:44:57 +0300 Subject: [PATCH 1/4] BWT complete --- hw1BWT/hw1BWT.sln | 25 +++++++++ hw1BWT/hw1BWT/BWT.cs | 101 ++++++++++++++++++++++++++++++++++++ hw1BWT/hw1BWT/Program.cs | 12 +++++ hw1BWT/hw1BWT/hw1BWT.csproj | 8 +++ 4 files changed, 146 insertions(+) create mode 100644 hw1BWT/hw1BWT.sln create mode 100644 hw1BWT/hw1BWT/BWT.cs create mode 100644 hw1BWT/hw1BWT/Program.cs create mode 100644 hw1BWT/hw1BWT/hw1BWT.csproj 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..733626f --- /dev/null +++ b/hw1BWT/hw1BWT/BWT.cs @@ -0,0 +1,101 @@ +using System; +using System.Linq; +using System.Collections.Generic; +using System.Text; + +namespace hw1BWT +{ + class BWT + { + static void SwapStrings(char[,] transpositionTable, int i, int j) + { + int size = transpositionTable.GetLength(0); + for (int t = 0; t < size; ++t) + { + char swapHelper = transpositionTable[i, t]; + transpositionTable[i, t] = transpositionTable[j, t]; + transpositionTable[j, t] = swapHelper; + } + } + + private static bool CompareStrs(char[,] table, int i, int j) + { + for (int l = 0; l < table.GetLength(0); ++l) + { + if (table[i, l] > (table[j, l])) + { + return true; + } + else if (table[i, l] < (table[j, l])) + { + return false; + } + } + return false; + } + + static void SortTable(char[,] table) + { + int size = table.GetLength(0); + for (int i = 0; i < size; ++i) + { + for (int j = size - 1; j > i; --j) + { + if (CompareStrs(table, i, j)) + { + SwapStrings(table, i, j); + } + } + } + } + + public static Tuple BWTransform(string str) + { + int size = str.Length; + char[,] table = new char[size, size]; + for (int i = 0; i < size; ++i) + { + table[0, i] = str[i]; + } + for (int i = 1; i < size; ++i) + { + table[i, 0] = table[i - 1, size - 1]; + for (int j = 1; j < size; ++j) + { + table[i, j] = table[i - 1, j - 1]; + } + } + SortTable(table); + int numberOfStr = 0; + for (int i = 0; i < size; ++i) + { + int count = 0; + if (table[i, 0] != str[0]) + { + continue; + } + for (int j = 0; j < size; ++j) + { + if (table[i, j] != str[j]) + { + break; + } + count++; + } + if (count == size) + { + numberOfStr = i; + break; + } + } + + var resultString = new char[size]; + for (int i = 0; i < size; i++) + { + resultString[i] = table[i, size - 1]; + } + var result = new String(resultString); + return new Tuple(result, numberOfStr); + } + } +} diff --git a/hw1BWT/hw1BWT/Program.cs b/hw1BWT/hw1BWT/Program.cs new file mode 100644 index 0000000..cfe6c76 --- /dev/null +++ b/hw1BWT/hw1BWT/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace hw1BWT +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} 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 + + + From 67d6964948b407632812a1859d8ca102dfce4f1e Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Fri, 5 Mar 2021 23:38:15 +0300 Subject: [PATCH 2/4] add ReverseBWT and Test --- hw1BWT/hw1BWT/BWT.cs | 82 +++++++++++++++++++++++++++++++++++++--- hw1BWT/hw1BWT/Program.cs | 18 +++++++-- 2 files changed, 91 insertions(+), 9 deletions(-) diff --git a/hw1BWT/hw1BWT/BWT.cs b/hw1BWT/hw1BWT/BWT.cs index 733626f..f1797f8 100644 --- a/hw1BWT/hw1BWT/BWT.cs +++ b/hw1BWT/hw1BWT/BWT.cs @@ -1,13 +1,22 @@ using System; using System.Linq; -using System.Collections.Generic; -using System.Text; namespace hw1BWT { class BWT { - static void SwapStrings(char[,] transpositionTable, int i, int j) + 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 void SwapStrings(char[,] transpositionTable, int i, int j) { int size = transpositionTable.GetLength(0); for (int t = 0; t < size; ++t) @@ -34,7 +43,7 @@ private static bool CompareStrs(char[,] table, int i, int j) return false; } - static void SortTable(char[,] table) + private static void SortTable(char[,] table) { int size = table.GetLength(0); for (int i = 0; i < size; ++i) @@ -49,7 +58,7 @@ static void SortTable(char[,] table) } } - public static Tuple BWTransform(string str) + public static Tuple BWTtransform(string str) { int size = str.Length; char[,] table = new char[size, size]; @@ -97,5 +106,66 @@ public static Tuple BWTransform(string str) var result = new String(resultString); return new Tuple(result, numberOfStr); } + + 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[] alphaberArray = alphabet.ToCharArray(0, alphabet.Length); + Array.Sort(alphaberArray); + return alphaberArray; + } + + 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 index cfe6c76..35024f6 100644 --- a/hw1BWT/hw1BWT/Program.cs +++ b/hw1BWT/hw1BWT/Program.cs @@ -1,12 +1,24 @@ using System; +using System.Linq; namespace hw1BWT { class Program { - static void Main(string[] args) + static void Main() { - Console.WriteLine("Hello World!"); + 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 From 51e6ab0effe6b279de2f5f69572591d5fc95e360 Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Mon, 5 Apr 2021 18:45:26 +0300 Subject: [PATCH 3/4] change bwt --- hw1BWT/hw1BWT/BWT.cs | 103 ++++++++++++------------------------------- 1 file changed, 28 insertions(+), 75 deletions(-) diff --git a/hw1BWT/hw1BWT/BWT.cs b/hw1BWT/hw1BWT/BWT.cs index f1797f8..c5893a3 100644 --- a/hw1BWT/hw1BWT/BWT.cs +++ b/hw1BWT/hw1BWT/BWT.cs @@ -3,7 +3,7 @@ namespace hw1BWT { - class BWT + static class BWT { public static bool Test() { @@ -14,97 +14,50 @@ public static bool Test() return false; } string oldString1 = ReverseBWT("cbaab", numberOfString); - return (oldString1 == string1); - } - private static void SwapStrings(char[,] transpositionTable, int i, int j) - { - int size = transpositionTable.GetLength(0); - for (int t = 0; t < size; ++t) - { - char swapHelper = transpositionTable[i, t]; - transpositionTable[i, t] = transpositionTable[j, t]; - transpositionTable[j, t] = swapHelper; - } + return oldString1 == string1; } - private static bool CompareStrs(char[,] table, int i, int j) + private static int[] WriteSuffixArray(string str) { - for (int l = 0; l < table.GetLength(0); ++l) + var suffixArray = new int[str.Length]; + for (int i = 0; i < str.Length; i++) { - if (table[i, l] > (table[j, l])) - { - return true; - } - else if (table[i, l] < (table[j, l])) - { - return false; - } + suffixArray[i] = i; } - return false; + return suffixArray; } - private static void SortTable(char[,] table) + public static Tuple BWTtransform(string str) { - int size = table.GetLength(0); - for (int i = 0; i < size; ++i) + var suffixArray = WriteSuffixArray(str); + for (int i = 1; i < suffixArray.Length; i++) { - for (int j = size - 1; j > i; --j) + for (int j = 0; j < suffixArray.Length - i; j++) { - if (CompareStrs(table, 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) { - SwapStrings(table, i, j); + int swaper = suffixArray[j]; + suffixArray[j] = suffixArray[j + 1]; + suffixArray[j + 1] = swaper; } } } - } - - public static Tuple BWTtransform(string str) - { - int size = str.Length; - char[,] table = new char[size, size]; - for (int i = 0; i < size; ++i) + string result = ""; + var strSize = str.Length; + for (int i = 0; i < suffixArray.Length; i++) { - table[0, i] = str[i]; - } - for (int i = 1; i < size; ++i) - { - table[i, 0] = table[i - 1, size - 1]; - for (int j = 1; j < size; ++j) + if (suffixArray[i] == 0) { - table[i, j] = table[i - 1, j - 1]; + result += str[strSize - 1]; } - } - SortTable(table); - int numberOfStr = 0; - for (int i = 0; i < size; ++i) - { - int count = 0; - if (table[i, 0] != str[0]) - { - continue; - } - for (int j = 0; j < size; ++j) - { - if (table[i, j] != str[j]) - { - break; - } - count++; - } - if (count == size) + else { - numberOfStr = i; - break; + result += str[suffixArray[i] - 1]; } } - - var resultString = new char[size]; - for (int i = 0; i < size; i++) - { - resultString[i] = table[i, size - 1]; - } - var result = new String(resultString); - return new Tuple(result, numberOfStr); + return new Tuple(result, Array.IndexOf(suffixArray, 0)); } private static char[] GetAlphabet(char[] str) @@ -117,9 +70,9 @@ private static char[] GetAlphabet(char[] str) alphabet += str[i]; } } - char[] alphaberArray = alphabet.ToCharArray(0, alphabet.Length); - Array.Sort(alphaberArray); - return alphaberArray; + char[] alphabetArray = alphabet.ToCharArray(0, alphabet.Length); + Array.Sort(alphabetArray); + return alphabetArray; } public static string ReverseBWT(string str, int numberOfStr) From f9c14ebc5ecd136654d00011bfc6edf9d305037a Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Mon, 5 Apr 2021 19:00:05 +0300 Subject: [PATCH 4/4] appveyor --- appveyor.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 appveyor.yml 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