How to resolve the algorithm Rep-string step by step in the Ada programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Rep-string step by step in the Ada programming language
Table of Contents
Problem Statement
Given a series of ones and zeroes in a string, define a repeated string or rep-string as a string which is created by repeating a substring of the first N characters of the string truncated on the right to the length of the input string, and in which the substring appears repeated at least twice in the original. For example, the string 10011001100 is a rep-string as the leftmost four characters of 1001 are repeated three times and truncated on the right to give the original string. Note that the requirement for having the repeat occur two or more times means that the repeating unit is never longer than half the length of the input string.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Rep-string step by step in the Ada programming language
Source code in the ada programming language
with Ada.Command_Line, Ada.Text_IO, Ada.Strings.Fixed;
procedure Rep_String is
function Find_Largest_Rep_String(S:String) return String is
L: Natural := S'Length;
begin
for I in reverse 1 .. L/2 loop
declare
use Ada.Strings.Fixed;
T: String := S(S'First .. S'First + I-1); -- the first I characters of S
U: String := (1+(L/I)) * T; -- repeat T so often that U'Length >= L
begin -- compare first L characers of U with S
if U(U'First .. U'First + S'Length -1) = S then
return T; -- T is a rep-string
end if;
end;
end loop;
return ""; -- no rep string;
end Find_Largest_Rep_String;
X: String := Ada.Command_Line.Argument(1);
Y: String := Find_Largest_Rep_String(X);
begin
if Y="" then
Ada.Text_IO.Put_Line("No rep-string for """ & X & """");
else
Ada.Text_IO.Put_Line("Longest rep-string for """& X &""": """& Y &"""");
end if;
end Rep_String;
You may also check:How to resolve the algorithm Parsing/Shunting-yard algorithm step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm Rot-13 step by step in the OCaml programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the JavaScript programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Red programming language
You may also check:How to resolve the algorithm Maximum triangle path sum step by step in the D programming language