Advertisement
Sephinroth

prac 12 prefinal

Dec 12th, 2019
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. // Исходная строка – 10000 символов из романа «Война и мир», искомая строка: а) «князь» б) «императрица»
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.IO;
  8. using System.Diagnostics;
  9.  
  10. namespace ConsoleApplication2
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             using (StreamReader input = new StreamReader("E:/practice/input.txt", Encoding.GetEncoding(1251)))
  17.             {
  18.                 string text = input.ReadToEnd();
  19.                 Stopwatch timer = new Stopwatch();
  20.                 for (int q = 0; q < 2; q++)
  21.                 {
  22.                     timer.Start();
  23.                     Console.WriteLine("Введите искомое слово: ");
  24.                
  25.                 string s = Console.ReadLine();
  26.                 const long P = 37;
  27.                 long[] array_P = new long[text.Length];
  28.                 array_P[0] = 1;
  29.                 for (int i = 1; i < text.Length; i++)
  30.                     array_P[i] = array_P[i - 1] * P;
  31.                 long[] array_hash = new long[text.Length];
  32.                 for (int i = 0; i < text.Length; i++)
  33.                 {
  34.                     array_hash[i] = (text[i] - 'a' + 1) * array_P[i]; //хэш текста
  35.                     if (i > 0)
  36.                         array_hash[i] += array_hash[i - 1];
  37.                 }
  38.                 long hash_s = 0;
  39.                 for (int i = 0; i < s.Length; i++)
  40.                     hash_s += (s[i] - 'a' + 1) * array_P[i]; //хэш подстроки
  41.  
  42.                 //проводим поиск по хеш-значениям
  43.                
  44.                 using (StreamWriter output = new StreamWriter("E:/practice/output.txt", true))
  45.                 {
  46.                     bool empty = true;
  47.                     for (int i = 0; i + s.Length - 1 < text.Length; i++)
  48.                     {
  49.                         // находим хэш-значение подстроки T начиная с позиции i длиною s.Length
  50.                         long cur_h = array_hash[i + s.Length - 1];
  51.                         if (i > 0)
  52.                             cur_h -= array_hash[i - 1];
  53.                         //приводим хэш-значения двух подстрок к одной степени
  54.                         if (cur_h == hash_s * array_P[i]) // если хеш-значения равны, то и подстроки равны
  55.                         {
  56.                             empty = false;
  57.                             output.WriteLine("Слово {0} найдено на {1}", s, i); // выводим позицию i, с которой начинается повторение
  58.                         }
  59.                     }
  60.                         if (empty)
  61.                             output.WriteLine("Слов не найдено");
  62.                         timer.Stop();
  63.                         output.WriteLine(timer.ElapsedMilliseconds);
  64.                     }
  65.                        
  66.                     }
  67.             }
  68.                 }
  69.             }
  70.  
  71.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement