Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Programmeeropdracht_6
- {
- class Program
- {
- static void Main(string[] args)
- {
- int aantal;
- // Aantal zinnen om in te zoeken
- aantal = Int32.Parse(Console.ReadLine());
- string[] zinnetjes = new string[aantal];
- for (int i = 0; i < aantal; i++)
- {
- zinnetjes[i] = Console.ReadLine();
- }
- for (int i = 0; i < aantal; i++)
- {
- Tricky.Vertex min = new Tricky.Vertex();
- min = Tricky.BFS(zinnetjes[i]);
- Console.WriteLine(min.Dist.ToString() + " " + min.Action);
- }
- // Zodat cmd blijft staan
- Console.ReadLine();
- }
- }
- class Tricky
- {
- // Methode voor Breadth-First Search
- public static Vertex BFS (string A)
- {
- // Nieuwe string aanmaken en sorteren om als referentie te gebruiken
- string Sorted = "";
- Sorted = String.Concat(A.OrderBy(c => c));
- // Aanmaken eerste vertex S
- Vertex S = new Vertex();
- S.Key = A;
- S.Dist = 0;
- S.Parent = null;
- // Aanmaken hashset met strings om te checken of deze al in de queue zit
- HashSet<string> tamos = new HashSet<string>();
- // Aanmaken Queue om alle te onderzoeken vertices in te stoppen
- Queue<Vertex> Q = new Queue<Vertex>();
- Q.Enqueue(S);
- // Doorgaan zolang er elementen in de queue zitten
- while (Q.Count != 0)
- {
- // Aanmaken vertex U
- Vertex U = new Vertex();
- U = Q.Dequeue();
- // Breadth-First zoeken
- for (int i = 1; i < 4; i++)
- {
- if (i == 1)
- {
- // Aanmaken vertex V
- Vertex V = new Vertex();
- V.Key = doA(S.Key);
- // Als Key zelfde is als gesorteerde string, return dan vertex
- if (V.Key == Sorted)
- {
- V.Dist = U.Dist + 1;
- V.Action += 'a';
- return V;
- }
- // Checken of-ie niet in hashset zit, anders toevoegen en in queue stoppen
- if (!tamos.Contains(V.Key))
- {
- // V.Key toevoegen aan hashset en V alle waardes geven
- tamos.Add(V.Key);
- V.Dist = U.Dist + 1;
- V.Action += 'a';
- V.Parent = U;
- Q.Enqueue(V);
- }
- }
- if (i == 2)
- {
- // Aanmaken vertex V
- Vertex V = new Vertex();
- V.Key = doB(S.Key);
- // Als Key zelfde is als gesorteerde string, return dan vertex
- if (V.Key == Sorted)
- {
- V.Dist = U.Dist + 1;
- V.Action += 'b';
- return V;
- }
- // Checken of-ie niet in hashset zit, anders toevoegen en in queue stoppen
- if (!tamos.Contains(V.Key))
- {
- // V.Key toevoegen aan hashset en V alle waardes geven
- tamos.Add(V.Key);
- V.Dist = U.Dist + 1;
- V.Action += 'b';
- V.Parent = U;
- Q.Enqueue(V);
- }
- }
- if (i == 3)
- {
- // Aanmaken vertex V
- Vertex V = new Vertex();
- V.Key = doX(S.Key);
- // Als Key zelfde is als gesorteerde string, return dan vertex
- if (V.Key == Sorted)
- {
- V.Dist = U.Dist + 1;
- V.Action += 'x';
- return V;
- }
- // Checken of-ie niet in hashset zit, anders toevoegen en in queue stoppen
- if (!tamos.Contains(V.Key))
- {
- // V.Key toevoegen aan hashset en V alle waardes geven
- tamos.Add(V.Key);
- V.Dist = U.Dist + 1;
- V.Action += 'x';
- V.Parent = U;
- Q.Enqueue(V);
- }
- }
- }
- }
- // Return de vertex
- return S;
- }
- public class Vertex
- {
- public Vertex Parent;
- public string Key;
- public int Dist;
- public string Action;
- }
- // Methode om actie "A" uit te voeren
- public static string doA(string S)
- {
- // Nieuwe string aanmaken en eerste twee letters omdraaien
- string a = "";
- a += S[1];
- a += S[0];
- for (int i = 2; i < S.Length; i++)
- a += S[i];
- // Return nieuwe string
- return a;
- }
- // Methode om actie "B" uit te voeren
- public static string doB (string S)
- {
- // Nieuwe string aanmaken en laatste twee letters omdraaien
- string b = "";
- for (int i = 0; i < S.Length - 2; i++)
- b += S[i];
- b += S[S.Length - 1];
- b += S[S.Length - 2];
- // Return nieuwe string
- return b;
- }
- // Methode om actie "X" uit te voeren
- public static string doX (string S)
- {
- // Nieuwe string aanmaken en alle middelste letters rechtsom roteren
- string x = "";
- x += S[0];
- x += S[S.Length - 2];
- for (int i = 1; i < S.Length - 2; i++)
- x += S[i];
- x += S[S.Length - 1];
- // Return nieuwe string
- return x;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement