diff --git a/CourseApp.Tests/CourseApp.Tests.csproj b/CourseApp.Tests/CourseApp.Tests.csproj index ae46394..79653f6 100644 --- a/CourseApp.Tests/CourseApp.Tests.csproj +++ b/CourseApp.Tests/CourseApp.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp2.1 + netcoreapp3.1 True 1573,1591,1701;1702;1705 false diff --git a/CourseApp/CourseApp.csproj b/CourseApp/CourseApp.csproj index 9551450..3634a9e 100644 --- a/CourseApp/CourseApp.csproj +++ b/CourseApp/CourseApp.csproj @@ -1,23 +1,18 @@ - - + Exe - netcoreapp2.1 + netcoreapp3.1 True 1573,1591,1701;1702;1705; - - - - - - ../_stylecop/stylecop.ruleset + ../_stylecop/stylecop.ruleset true - - + + + + - - + \ No newline at end of file diff --git a/CourseApp/Module3/CyclicShift.cs b/CourseApp/Module3/CyclicShift.cs new file mode 100644 index 0000000..6a240b0 --- /dev/null +++ b/CourseApp/Module3/CyclicShift.cs @@ -0,0 +1,74 @@ +using System; +using System.IO; +using System.Linq; +using System.Collections.Generic; + +namespace CourseApp.Module3 +{ + public class CyclicShift + { + public static void FindStringEntry() + { + StreamReader reader = new StreamReader("input.txt"); + string data = reader.ReadLine(); + string key = reader.ReadLine(); + reader.Close(); + + long simp_numb = 9999993; + int alphabet = 26; + int count = RabinKarpAlgorythm(data, key, alphabet, simp_numb); + StreamWriter output = new StreamWriter("output.txt"); + output.WriteLine(count); + output.Close(); + } + + public static int RabinKarpAlgorythm(string text, string key, int alphabet, long simp_numb) + { + int count = 0; + long basis = 1; + int size = text.Length; + text = text + text; + for (int i = 0; i < size - 1; i++) + { + basis = (basis * alphabet) % simp_numb; + } + + long hash_key = 0; + long hash_text = 0; + for (int i = size - 1; i >= 0; i--) + { + hash_key = ((hash_key * alphabet) + key[i]) % simp_numb; + hash_text = ((hash_text * alphabet) + text[i]) % simp_numb; + } + + for (int i = size; i >= 0; i--) + { + bool trigger = true; + if (hash_key == hash_text) + { + for (int j = 0; j < size; j++) + { + if (key[j] != text[i + j]) + { + trigger = false; + } + } + + if (trigger) + { + return count; + } + } + + count++; + if (i != 0) + { + hash_text = ((alphabet * (hash_text - (text[i + size - 1] * basis))) + text[i - 1]) % simp_numb; + hash_text = (hash_text + simp_numb) % simp_numb; + } + } + + return -1; + } + } +} \ No newline at end of file diff --git a/CourseApp/Module3/CyclicSubString.cs b/CourseApp/Module3/CyclicSubString.cs new file mode 100644 index 0000000..cfb2365 --- /dev/null +++ b/CourseApp/Module3/CyclicSubString.cs @@ -0,0 +1,45 @@ +using System; +using System.IO; +using System.Linq; +using System.Collections.Generic; + +namespace CourseApp.Module3 +{ + public class CyclicSubString + { + public static void FindStringEntry() + { + StreamReader reader = new StreamReader("input.txt"); + string data = reader.ReadLine(); + reader.Close(); + StreamWriter output = new StreamWriter("output.txt"); + output.WriteLine(data.Length - Prefix(data)[data.Length - 1]); + output.Close(); + } + + public static int[] Prefix(string input) + { + int[] result = new int[input.Length]; + result[0] = 0; + for (int i = 0; i < input.Length - 1; i++) + { + int buffer = result[i]; + while (buffer > 0 && input[i + 1] != input[buffer]) + { + buffer = result[buffer - 1]; + } + + if (input[i + 1] == input[buffer]) + { + result[i + 1] = buffer + 1; + } + else + { + result[i + 1] = 0; + } + } + + return result; + } + } +} \ No newline at end of file diff --git a/CourseApp/Module3/RabinKarp.cs b/CourseApp/Module3/RabinKarp.cs new file mode 100644 index 0000000..9c1df26 --- /dev/null +++ b/CourseApp/Module3/RabinKarp.cs @@ -0,0 +1,73 @@ +using System; +using System.IO; +using System.Linq; +using System.Collections.Generic; + +namespace CourseApp.Module3 +{ + public class RabinKarp + { + public static void FindStringEntry() + { + StreamReader reader = new StreamReader("input.txt"); + string data = reader.ReadLine(); + string pattern = reader.ReadLine(); + reader.Close(); + + int simp_numb = 117; + int alphabet = 26; + List index = new List(); + RabinKarpAlgorythm(index, data, pattern, alphabet, simp_numb); + StreamWriter output = new StreamWriter("output.txt"); + output.WriteLine(string.Join(" ", index)); + output.Close(); + } + + public static void RabinKarpAlgorythm(List index, string text, string pattern, int alphabet, int simp_numb) + { + int basis = 1; + int txt_size = text.Length; + int pat_size = pattern.Length; + for (int i = 0; i < pat_size - 1; i++) + { + basis = (basis * alphabet) % simp_numb; + } + + int hash_pat = 0; + int hash_text = 0; + for (int i = 0; i < pat_size; i++) + { + hash_pat = ((hash_pat * alphabet) + pattern[i]) % simp_numb; + hash_text = ((hash_text * alphabet) + text[i]) % simp_numb; + } + + bool trigger = true; + for (int i = 0; i <= txt_size - pat_size; i++) + { + trigger = true; + if (hash_pat == hash_text) + { + for (int j = 0; j < pat_size; j++) + { + if (pattern[j] != text[i + j]) + { + trigger = false; + break; + } + } + + if (trigger) + { + index.Add(i); + } + } + + if (i < txt_size - pat_size) + { + hash_text = ((alphabet * (hash_text - (text[i] * basis))) + text[i + pat_size]) % simp_numb; + hash_text = (hash_text + simp_numb) % simp_numb; + } + } + } + } +} \ No newline at end of file diff --git a/CourseApp/Module3/StringPeriod.cs b/CourseApp/Module3/StringPeriod.cs new file mode 100644 index 0000000..04e053e --- /dev/null +++ b/CourseApp/Module3/StringPeriod.cs @@ -0,0 +1,73 @@ +using System.IO; + +namespace CourseApp.Module3 +{ + public class StringPeriod + { + public static void FindStringEntry() + { + StreamReader reader = new StreamReader("input.txt"); + string data = reader.ReadLine(); + reader.Close(); + + long simp_numb = 9999993; + int alphabet = 26; + int count = GetCountOfRepeating(data, alphabet, simp_numb); + StreamWriter output = new StreamWriter("output.txt"); + output.WriteLine(count); + output.Close(); + } + + public static int GetCountOfRepeating(string text, int alphabet, long simp_numb) + { + int size = text.Length; + int count = 1; + + for (int i = 2; i <= (size / 2) + 1; i++) + { + if (size % i != 0) + { + continue; + } + + count = CheckEqual(i, size, count, text, alphabet, simp_numb); + } + + count = CheckEqual(size, size, count, text, alphabet, simp_numb); + + return count; + } + + public static int CheckEqual(int i, int size, int count, string text, int alphabet, long simp_numb) + { + long[] buffer = new long[i]; + for (int j = 0; j < buffer.Length; j++) + { + long hash = 0; + for (int k = (size / i) * j; k < ((size / i) * j) + (size / i); k++) + { + hash = ((hash * alphabet) + text[k]) % simp_numb; + } + + buffer[j] = hash; + if (j > 0) + { + if (buffer[j] != buffer[j - 1]) + { + break; + } + } + } + + if (size > 1) + { + if ((buffer[buffer.Length - 1] != 0) && (buffer[buffer.Length - 1] == buffer[buffer.Length - 2])) + { + count = i; + } + } + + return count; + } + } +} \ No newline at end of file diff --git a/CourseApp/Program.cs b/CourseApp/Program.cs index cafa825..248fd51 100644 --- a/CourseApp/Program.cs +++ b/CourseApp/Program.cs @@ -1,5 +1,4 @@ -using System; -using CourseApp.Module2; +using CourseApp.Module3; namespace CourseApp { @@ -7,9 +6,7 @@ public class Program { public static void Main(string[] args) { - BubbleSort.BubbleSortMethod(); - - Console.WriteLine("Hello World"); + RabinKarp.FindStringEntry(); } } -} +} \ No newline at end of file diff --git a/CourseApp/input.txt b/CourseApp/input.txt new file mode 100644 index 0000000..593f459 --- /dev/null +++ b/CourseApp/input.txt @@ -0,0 +1,2 @@ +ababbababa +aba \ No newline at end of file diff --git a/CourseApp/output.txt b/CourseApp/output.txt new file mode 100644 index 0000000..c073551 --- /dev/null +++ b/CourseApp/output.txt @@ -0,0 +1,5 @@ +1 +2 +2 +3 +1