| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- using System;
- using System.IO;
- using System.Linq;
- namespace D5._2
- {
- class Program
- {
- static void Main(string[] args)
- {
- if (args.Length < 1) return;
- if (File.Exists(args[0]) == false) return;
- var file = File.OpenText(args[0]);
- string polymer = file.ReadLine();
- int shortest = polymer.Length;
- for (char i = 'A'; i <= 'Z'; i++)
- {
- var altera = alterPolymer(polymer, i);
- reduce(ref altera);
- if (altera.Length < shortest) shortest = altera.Length;
- }
- Console.WriteLine(shortest);
- }
- static void reduce(ref string polymer)
- {
- for (int i = 1; i < polymer.Length; i++)
- {
- char cc = polymer[i];
- char cm = polymer[i - 1];
- if (Math.Abs(cc - cm) <= 26) continue;
- if (up(cc) == up(cm))
- {
- polymer = polymer.Remove(i - 1, 2);
- i = Math.Max(0, i - 2);
- }
- }
- }
- static string alterPolymer(string polymer, char unit)
- {
- string newPolymer = string.Empty;
- foreach (var car in polymer)
- {
- if (up(car) == up(unit)) continue;
- newPolymer += car;
- }
- return newPolymer;
- }
- static char up(char c)
- {
- if (c >= 'A' && c <= 'Z') return c;
- return (char)('A' + (c - 'a'));
- }
- }
- }
|