-
Notifications
You must be signed in to change notification settings - Fork 0
Hw1 bwt #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Hw1 bwt #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| image: Visual Studio 2019 | ||
|
|
||
| build_script: | ||
| - For /R %%I in (*.sln) do dotnet test %%I | ||
|
|
||
| test: off |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<string, int> 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]); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Substring каждый раз создаёт новую строку, так что это может быть очень серьёзным ударом по потребляемой памяти. Тут лучше было руками написать "циклическое сравнение с заданного индекса" |
||
| if (String.Compare(str1, str2) > 0) | ||
| { | ||
| int swaper = suffixArray[j]; | ||
| suffixArray[j] = suffixArray[j + 1]; | ||
| suffixArray[j + 1] = swaper; | ||
| } | ||
| } | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. И потом, квадратичная сортировка тут может быть не очень, потому что строки бывают длинные. В идеале надо было реализовать свой IComparer и воспользоваться библиотечным Array.Sort, передав ему компаратор. |
||
| 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]; | ||
| } | ||
| } | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
| return new Tuple<string, int>(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; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>netcoreapp3.1</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BWTTransform, или BwtTransform, в CamelCase каждое слово должно быть с заглавной