Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Diagnostics;
- namespace ForDZ
- {
- static class pr_12_15
- {
- private static void generate_input1()
- {
- StreamWriter writer = new StreamWriter("input.txt");
- Random random = new Random();
- int n = 10000;
- for (int a = 0; a < n; a++)
- {
- writer.Write(random.Next(1, 10));
- }
- writer.Close();
- }
- private static void generate_input2()
- {
- StreamWriter writer = new StreamWriter("input.txt");
- Random random = new Random();
- int n = 100000;
- for (int a = 0; a < n; a++)
- {
- writer.Write(random.Next(1, 10));
- }
- writer.Close();
- }
- static long GetHash(long[] h, int L, int R)
- {
- if (L > 0) return h[R] - h[L - 1];
- return h[R];
- }
- static long GetHashRevers(long[] h_r, int L, int R)
- {
- if (R < h_r.Length - 1) return h_r[L] - h_r[R + 1];
- return h_r[L];
- }
- static bool IsPalindrome(long[] h, long[] h_r, long[] pwp, int L, int R)
- {
- return GetHash(h, L, R) * pwp[h.Length - R - 1] == GetHashRevers(h_r, L, R) * pwp[L];
- }
- static void Print(int[] oddCount, int[] evenCount, string s)
- {
- StreamWriter writer = new StreamWriter("input.txt");
- Console.WriteLine("Палиндромы:");
- writer.WriteLine("Палиндромы:");
- string t;
- int max = 0;
- for (int i = 0; i < oddCount.Length; ++i)
- {
- if (oddCount[i] > max)
- {
- max = oddCount[i];
- }
- }
- Console.WriteLine($"> {max * 2 - 1}:");
- writer.WriteLine($"> {max * 2 - 1}:");
- for (int i = 0; i < oddCount.Length; ++i)
- {
- if (oddCount[i] == max)
- {
- t = s.Substring(i - oddCount[i] + 1, oddCount[i] * 2 - 1);
- Console.WriteLine(t);
- writer.WriteLine(t);
- }
- }
- max = 0;
- for (int i = 0; i < evenCount.Length; ++i)
- {
- if (evenCount[i] > max)
- {
- max = evenCount[i];
- }
- }
- Console.WriteLine($"> {max * 2}:");
- writer.WriteLine($"> {max * 2}:");
- for (int i = 0; i < evenCount.Length; ++i)
- {
- if (evenCount[i] == max)
- {
- t = s.Substring(i - evenCount[i], evenCount[i] * 2);
- Console.WriteLine(t);
- writer.WriteLine(t);
- }
- }
- writer.Close();
- }
- private static void func(string s)
- {
- int n = s.Length;
- const long P = 11;
- long[] pwp = new long[n];
- pwp[0] = 1;
- for (int i = 1; i < n; i++)
- {
- pwp[i] = pwp[i - 1] * P;
- }
- long[] h = new long[n];
- long[] h_r = new long[n];
- int[] oddCount = new int[n];
- int[] evenCount = new int[n];
- Stopwatch timer = new Stopwatch();
- timer.Start();
- h[0] = (s[0] - '0') * pwp[0];
- h_r[n - 1] = (s[n - 1] - '0') * pwp[0];
- for (int i = 1; i < n; i++)
- {
- h[i] = (s[i] - '0') * pwp[i];
- h_r[n - 1 - i] = (s[n - 1 - i] - '0') * pwp[i];
- h[i] += h[i - 1];
- h_r[n - 1 - i] += h_r[n - i];
- }
- for (int i = 0; i < n; i++)
- {
- int left = 1, right = Math.Min(i + 1, n - i);
- while (left <= right)
- {
- int middle = (left + right) / 2;
- if (IsPalindrome(h, h_r, pwp, i - middle + 1, i + middle - 1))
- {
- oddCount[i] = middle;
- left = middle + 1;
- }
- else
- {
- right = middle - 1;
- }
- }
- }
- for (int i = 0; i < n; i++)
- {
- int left = 1, right = Math.Min(i, n - i);
- while (left <= right)
- {
- int middle = (left + right) / 2;
- if (IsPalindrome(h, h_r, pwp, i - middle, i + middle - 1))
- {
- evenCount[i] = middle;
- left = middle + 1;
- }
- else
- {
- right = middle - 1;
- }
- }
- }
- timer.Stop();
- Console.WriteLine($"{timer.ElapsedMilliseconds}ms ~ {timer.ElapsedTicks}tics");
- //Print(oddCount, evenCount, s);
- }
- public static void Task()
- {
- StreamReader reader;
- StringBuilder input = new StringBuilder();
- Console.WriteLine("> 10000 : ");
- for (int i = 0; i < 10; i++)
- {
- generate_input1();
- reader = new StreamReader("input.txt");
- input.Clear();
- while (!reader.EndOfStream)
- {
- input.Append(reader.ReadLine());
- }
- reader.Close();
- func(input.ToString());
- }
- Console.WriteLine("> 100000 : ");
- for (int i = 0; i < 10; i++)
- {
- generate_input2();
- reader = new StreamReader("input.txt");
- input.Clear();
- while (!reader.EndOfStream)
- {
- input.Append(reader.ReadLine());
- }
- reader.Close();
- func(input.ToString());
- }
- }
- }
- static class pr_14_I_18
- {
- struct SPoint
- {
- public int X { get; private set; }
- public int Y { get; private set; }
- public int Z { get; private set; }
- public SPoint(int x, int y, int z)
- {
- X = x;
- Y = y;
- Z = z;
- }
- public int GetSqrLength(SPoint point)
- {
- int tx = point.X - X;
- int ty = point.Y - Y;
- int tz = point.Z - Z;
- return tx * tx + ty * ty + tz * tz;
- }
- public override string ToString()
- {
- return string.Format($"({X}, {Y}, {Z})");
- }
- }
- private static int GetPerimeterOfSqr(SPoint p1, SPoint p2, SPoint p3)
- {
- int l1 = p1.GetSqrLength(p2);
- int l2 = p2.GetSqrLength(p3);
- int l3 = p3.GetSqrLength(p1);
- return l1 + l2 + l3;
- }
- private static void generate_input()
- {
- StreamWriter writer = new StreamWriter("input.txt");
- Random random = new Random();
- int n = 3;
- for (int a = 0; a <= n; a++)
- {
- for (int b = 0; b <= n; b++)
- {
- for (int c = 0; c <= n; c++)
- {
- writer.WriteLine($"{a} {b} {c}");
- }
- }
- }
- writer.Close();
- }
- struct Triangle
- {
- public SPoint a, b, c;
- public Triangle(SPoint a, SPoint b, SPoint c)
- {
- this.a = a;
- this.b = b;
- this.c = c;
- }
- }
- public static void Task()
- {
- generate_input();
- StreamReader reader = new StreamReader("input.txt");
- List<SPoint> input = new List<SPoint>();
- while (!reader.EndOfStream)
- {
- string s = reader.ReadLine();
- if (s.Length == 0) continue;
- int[] t_input_array = Array.ConvertAll(
- s.Trim().Split(
- new char[] { ' ' },
- StringSplitOptions.RemoveEmptyEntries),
- int.Parse);
- if (t_input_array.Length != 3) continue;
- SPoint t_point = new SPoint(
- t_input_array[0],
- t_input_array[1],
- t_input_array[2]);
- input.Add(t_point);
- }
- reader.Close();
- SPoint[] points = input.ToArray();
- Triangle max_triangle = new Triangle(points[0], points[0], points[0]);
- int max_perimeter = -1;
- for (int a = 0, a_end = points.Length - 2; a < a_end; a++)
- {
- for (int b = a + 1, b_end = points.Length - 1; b < b_end; b++)
- {
- for (int c = b + 1, c_end = points.Length; c < c_end; c++)
- {
- int t_perimeter = GetPerimeterOfSqr(points[a], points[b], points[c]);
- //Console.Write($"> (");
- //Console.Write($"{points[a].ToString()} ");
- //Console.Write($"{points[b].ToString()} ");
- //Console.Write($"{points[c].ToString()}) -> ");
- //Console.WriteLine(t_perimeter);
- if (t_perimeter > max_perimeter)
- {
- max_perimeter = t_perimeter;
- max_triangle = new Triangle(points[a], points[b], points[c]);
- }
- }
- }
- }
- Console.Write($"Result: ");
- Console.Write($"{max_triangle.a.ToString()} ");
- Console.Write($"{max_triangle.b.ToString()} ");
- Console.Write($"{max_triangle.c.ToString()} ");
- Console.WriteLine();
- }
- }
- static class pr_14_II_10
- {
- struct Student : IComparable<Student>
- {
- public string FIO { get; private set; }
- public int Groupe { get; private set; }
- public int Result { get; private set; }
- public Student(string n, string ln, string sn, int g, int r)
- {
- FIO = string.Format($"{n} {ln} {sn}");
- Groupe = g;
- Result = r;
- }
- public int CompareTo(Student student)
- {
- if (Result < student.Result) return -1;
- if (Result > student.Result) return 1;
- return 0;
- }
- public override string ToString()
- {
- return string.Format($"{FIO,30}, {Groupe} : {Result}");
- }
- }
- private static Student student_parse(string s)
- {
- string[] input = s.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- Student res = new Student(
- input[0], input[1], input[2],
- int.Parse(input[3]), int.Parse(input[4]));
- return res;
- }
- private static void generate_input()
- {
- StreamWriter writer = new StreamWriter("input.txt");
- Random random = new Random();
- int cnt = random.Next(200, 301);
- int n;
- StringBuilder builder = new StringBuilder(11);
- for (int oi = 0; oi < cnt; oi++)
- {
- for (int names = 0; names < 3; names++)
- {
- n = random.Next(3, 11);
- builder.Clear();
- builder.Append((char)random.Next((int)('А'), (int)('Я') + 1));
- for (int i = 0; i < n; i++)
- {
- builder.Append((char)random.Next((int)('а'), (int)('я') + 1));
- }
- builder.Append(" ");
- writer.Write(builder.ToString());
- }
- writer.Write($"{random.Next(111, 1000)} ");
- writer.WriteLine(random.Next(10, 100));
- }
- writer.Close();
- }
- public static void Task()
- {
- generate_input();
- StreamReader reader = new StreamReader("input.txt");
- List<Student> input = new List<Student>();
- while (!reader.EndOfStream)
- {
- input.Add(student_parse(reader.ReadLine()));
- }
- reader.Close();
- input.Sort();
- Console.WriteLine(input[0].ToString());
- for (int i = 1, cnt = 0; i < input.Count; i++)
- {
- if (input[i].Result != input[i - 1].Result)
- {
- cnt++;
- }
- if (cnt >= 3) break;
- Console.WriteLine(input[i].ToString());
- }
- Console.WriteLine();
- }
- }
- static class pr_15_I_20
- {
- private static void generate_input()
- {
- StreamWriter writer = new StreamWriter("input.txt");
- Random random = new Random();
- int n = 1000;
- for (int i = 0; i < n; i++)
- {
- writer.Write($"{random.Next(1, 1000)} ");
- }
- writer.Close();
- }
- public static void Task()
- {
- generate_input();
- StreamReader reader = new StreamReader("input.txt");
- List<int> input = new List<int>();
- char[] sep = { ' ' };
- while (!reader.EndOfStream)
- {
- input.AddRange(
- from s in
- reader.ReadLine().Trim().Split(sep,
- StringSplitOptions.RemoveEmptyEntries)
- select int.Parse(s));
- }
- reader.Close();
- StreamWriter writer = new StreamWriter("input.txt");
- foreach (int i in
- from n in input orderby n descending where n % 2 == 1 select 2 * n)
- {
- writer.Write($"{i} ");
- Console.Write($"{i} ");
- }
- writer.Close();
- Console.WriteLine();
- }
- }
- static class pr_15_II_20
- {
- struct Student
- {
- public string Name { get; private set; }
- public string Fak { get; private set; }
- public int Course { get; private set; }
- public int Groupe { get; private set; }
- public int Res1 { get; private set; }
- public int Res2 { get; private set; }
- public int Res3 { get; private set; }
- public Student(string n, string f, int c, int g, int r1, int r2, int r3)
- {
- Name = n;
- Fak = f;
- Course = c;
- Groupe = g;
- Res1 = r1;
- Res2 = r2;
- Res3 = r3;
- }
- public override string ToString()
- {
- return string.Format($"{Name,10}, {Fak,5}, {Course}, {Groupe} : {Res1}, {Res2}, {Res3}");
- }
- }
- private static Student student_parse(string s)
- {
- string[] input = s.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- Student res = new Student(
- input[0], input[1],
- int.Parse(input[2]),
- int.Parse(input[3]),
- int.Parse(input[4]),
- int.Parse(input[5]),
- int.Parse(input[6]));
- return res;
- }
- private static void generate_input()
- {
- StreamWriter writer = new StreamWriter("input.txt");
- Random random = new Random();
- int cnt = random.Next(200, 301);
- int n;
- StringBuilder builder = new StringBuilder(11);
- for (int oi = 0; oi < cnt; oi++)
- {
- n = random.Next(3, 11);
- builder.Clear();
- builder.Append((char)random.Next((int)('А'), (int)('Я') + 1));
- for (int i = 0; i < n; i++)
- {
- builder.Append((char)random.Next((int)('а'), (int)('я') + 1));
- }
- builder.Append(" ");
- writer.Write(builder.ToString());
- n = 5;
- builder.Clear();
- builder.Append((char)random.Next((int)('А'), (int)('Я') + 1));
- for (int i = 0; i < n; i++)
- {
- builder.Append((char)random.Next((int)('а'), (int)('я') + 1));
- }
- builder.Append(" ");
- writer.Write(builder.ToString());
- writer.Write($"{random.Next(1, 5)} ");
- writer.Write($"{random.Next(1, 10)} ");
- writer.Write($"{random.Next(2, 6)} ");
- writer.Write($"{random.Next(2, 6)} ");
- writer.WriteLine(random.Next(2, 6));
- }
- writer.Close();
- }
- public static void Task()
- {
- generate_input();
- StreamReader reader = new StreamReader("input.txt");
- List<Student> input = new List<Student>();
- char[] sep = { ' ' };
- while (!reader.EndOfStream)
- {
- input.Add(student_parse(reader.ReadLine().Trim()));
- }
- reader.Close();
- StreamWriter writer = new StreamWriter("input.txt");
- foreach (var students in
- from student in input
- orderby student.Course
- where student.Res1 == 5
- where student.Res2 == 5
- where student.Res3 == 5
- group student by student.Course)
- {
- writer.WriteLine($"> {students.Key}:");
- Console.WriteLine($"> {students.Key}:");
- foreach (Student student in students)
- {
- writer.WriteLine($"{student.ToString()}");
- Console.WriteLine($"{student.ToString()}");
- }
- }
- writer.Close();
- Console.WriteLine();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- //Console.BufferHeight = 3000;
- //pr_12_15.Task();
- pr_14_I_18.Task();
- //pr_14_II_10.Task();
- //pr_15_I_20.Task();
- //pr_15_II_20.Task();
- Console.WriteLine("\n\nend.");
- Console.ReadKey();
- }
- }
- }
Add Comment
Please, Sign In to add comment