using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication25 { abstract class Seznam { public int Pocet = 0; public abstract void Pridej(string s); } class SeznamVPoli : Seznam { int velikost; string[] pole; public SeznamVPoli(int velikost) { this.velikost = velikost; pole = new string[velikost]; Pocet = 0; } public override void Pridej(string s) { System.Diagnostics.Debug.Assert(Pocet < velikost, "Seznam uz je plny"); pole[Pocet] = s; int i = 0; while (pole[i] != s) i++; if (i == Pocet) Pocet++; } } class SeznamJakoHashovaciTabulka : Seznam { int velikost; string[] pole; public SeznamJakoHashovaciTabulka(int velikost) { this.velikost = velikost; pole = new string[velikost]; Pocet = 0; } int f(string s) { int suma = 0; for (int i = 0; i < s.Length; i++) { suma = (suma + 273 * s[i] * (s[i]-'A') * s[i]) % velikost; } return suma; } public override void Pridej(string s) { System.Diagnostics.Debug.Assert(Pocet < velikost, "Seznam uz je plny"); int i = f(s); while (true) { if (pole[i]==null) { pole[i] = s; Pocet++; return; } if (pole[i]==s) { return; } i = (1 + i) % velikost; } } } class Program { static Random rnd = new Random(1); static string NahodnyString() { string s = ""; for (int i = 0; i < 5; i++) { s = s + (char)(rnd.Next(26) + 'A'); } return s; } static void Main(string[] args) { //Seznam seznam = new SeznamVPoli(1000*1000); Seznam seznam = new SeznamJakoHashovaciTabulka(1000 * 1000*2); DateTime start = DateTime.Now; for (int i = 0; i < 1000*1000; i++) { seznam.Pridej( NahodnyString() ); if(i%10000==0) { DateTime ted = DateTime.Now; TimeSpan doba = ted - start; Console.WriteLine("{1}: {0} ms", doba.TotalMilliseconds,i); } } Console.WriteLine("Pocet = {0}",seznam.Pocet); } } }