1
0

Program.cs 1020 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.IO;
  3. namespace D5._1
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. if (args.Length < 1) return;
  10. if (File.Exists(args[0]) == false) return;
  11. var file = File.OpenText(args[0]);
  12. string polymer = file.ReadLine();
  13. Reduce(ref polymer);
  14. Console.WriteLine(polymer.Length);
  15. }
  16. static void Reduce(ref string polymer)
  17. {
  18. for (int i = 1; i < polymer.Length; i++)
  19. {
  20. char cc = polymer[i];
  21. char cm = polymer[i-1];
  22. if (Math.Abs(cc - cm) <= 26) continue;
  23. if (Up(cc) == Up(cm))
  24. {
  25. polymer = polymer.Remove(i - 1, 2);
  26. i = Math.Max(0, i - 2);
  27. }
  28. }
  29. }
  30. static char Up(char c)
  31. {
  32. if (c >= 'A' && c <= 'Z') return c;
  33. return (char) ('A' + (c - 'a'));
  34. }
  35. }
  36. }