From 2d6af925f4cf52c0c697916c3f22400964a75037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Sun, 16 Dec 2018 08:44:36 +0100 Subject: [PATCH] =?UTF-8?q?Andra=20problemet=20dag=205=20,=20l=C3=B6st?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2018_05/Program.cs | 56 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/2018_05/Program.cs b/2018_05/Program.cs index 2a0a3cd..708e61b 100644 --- a/2018_05/Program.cs +++ b/2018_05/Program.cs @@ -17,7 +17,7 @@ namespace _2018_05 //string resTab = "dabAcCaCBAcCcaDA"; SortedSet charset = new SortedSet(); - foreach(char x in resTab.ToCharArray()) + foreach (char x in resTab.ToCharArray()) { try { @@ -28,31 +28,71 @@ namespace _2018_05 } - foreach(char y in charset) + foreach (char y in charset) { Write($"{y},"); } ReadKey(); - int varv = 0; + string nextStr = StringReduce(ref removes, resTab); - string nextStr = nextLevelStr(resTab,ref removes, ref varv); - while (removes > 0) + int winner = nextStr.Length; + string wrkWin = ""; + foreach (char y in charset) { - nextStr= nextLevelStr(nextStr, ref removes, ref varv); + string wrk = ""; + wrk += y; + if (wrk != wrk.ToLower()) + { + string tmpResTab = ""; + string tmpResStr = ""; + int tmpRemoves = 0; + foreach (char z in resTab.ToCharArray()) + { + if (z.ToString() != wrk && z.ToString() != wrk.ToLower()) + { + tmpResTab += z; + } + } + tmpResStr = StringReduce(ref tmpRemoves, tmpResTab); + if (tmpResStr.Length < winner) + { + nextStr = tmpResStr; + winner = tmpResStr.Length; + wrkWin = wrk + wrk.ToLower(); + } + } } + WriteLine($"Resulterande sträng : {nextStr} , {nextStr.Length} tkn reducerad med {wrkWin}"); + ReadKey(); + + nextStr = StringReduce(ref removes, resTab); + WriteLine($"Resulterande sträng : {nextStr} , {nextStr.Length} tkn"); ReadKey(); - + + } + + private static string StringReduce(ref int removes, string resTab) + { + int varv = 0; + + string nextStr = nextLevelStr(resTab, ref removes, ref varv); + while (removes > 0) + { + nextStr = nextLevelStr(nextStr, ref removes, ref varv); + } + + return nextStr; } private static string nextLevelStr(string resTab,ref int removes, ref int varv) { string tmp = ""; removes = 0; - WriteLine($"inLängd: {resTab.Length}"); + //WriteLine($"inLängd: {resTab.Length}"); foreach(char tkn in resTab.ToCharArray()) { tmp += tkn;