Problem hidden
|This problem was hidden by Editorial Board member probably because it has incorrect language|version or invalid test data, or description of the problem is not clear.|

RGB9039 - Хоёр шаблон

Шаблон гэдэг нь англи цагаан толгойн үсгүүд ( a,...,z A,...,Z ) болон ? ба * тэмдгүүдээс тогтох тэмдэгт мөр юм.

? тэмдэг бүрийг ямар нэгэн үсгээр, * тэмдэг бүрийг дурын үсгүүдийн дарааллаар ( хоосон байж болно ) сольж үүссэн тэмдэг мөрийг уг шаблонд таарна гэж хэлдэг. Өгөгдсөн хоёр шаблонд зэрэг таарах хамгийн бага урттай тэмдэгт мөрийн уртыг ол.

Хэрвээ ийм тэмдэг мөр байхгүй бол энэ тухай мэдээл.

Input

Эхний 2 мөрөнд шаблонууд дан дангаараа өгөгдөнө.

Уртууд нь 80 тэмдэгтээс хэтрэхгүй.

Output

Хоёр шаблонд зэрэг таарах хамгийн бага урттай тэмдэгт мөрийн урт. Хэрвээ ийм тэмдэгт мөр байхгүй -1 гэж мэдээл.

Example

Input:
A*
B*
Output:
2

Нэмсэн:Bataa
Огноо:2010-02-11
Хугацааны хязгаарлалт:1s
Эх кодын хэмжээний хязгаарлалт:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Програмчлалын хэлүүд:ADA95 ASM32 ASM64 BASH BF C CSHARP C++ 4.3.2 CPP CPP14 C99 CLPS LISP sbcl LISP clisp D ERL FORTRAN HASK ICON ICK JAVA JS-RHINO LUA NEM NICE OCAML PAS-GPC PAS-FPC PERL PHP PIKE PRLG-swi PYPY RUBY SCALA SCM guile ST TCL TEXT WHITESPACE

hide comments
2024-05-22 04:22:24
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Strings.Bounded; use Ada.Strings.Bounded;

procedure Pattern_Matching is
subtype String_Type is String (1 .. 100);
Template1, Template2 : String_Type := (others => ' ');
Len1, Len2 : Natural := 0;

function Min (A, B : Natural) return Natural is
begin
if A < B then
return A;
else
return B;
end if;
end Min;

function Match_Template (T1, T2 : String_Type; Len1, Len2 : Natural) return Natural is
i, j, Len : Natural := 1;
T1_Filled, T2_Filled : Bounded_String (200);
begin
T1_Filled := To_Bounded_String ("");
T2_Filled := To_Bounded_String ("");

while i <= Len1 or j <= Len2 loop
if i <= Len1 and then T1 (i) /= '*' and then (j > Len2 or else T2 (j) = '*') then
Append (T1_Filled, T1 (i));
i := i + 1;
elsif j <= Len2 and then T2 (j) /= '*' and then (i > Len1 or else T1 (i) = '*') then
Append (T2_Filled, T2 (j));
j := j + 1;
elsif i <= Len1 and then j <= Len2 and then T1 (i) = T2 (j) and then T1 (i) /= '*' then
Append (T1_Filled, T1 (i));
Append (T2_Filled, T2 (j));
i := i + 1;
j := j + 1;
elsif i <= Len1 and then T1 (i) = '*' then
i := i + 1;
elsif j <= Len2 and then T2 (j) = '*' then
j := j + 1;
else
return Natural'Last;
end if;
end loop;

Len := Min (Length (T1_Filled), Length (T2_Filled));
return Len;
end Match_Template;

begin
-- Input templates
Put_Line ("Enter the first template:");
Get_Line (Template1, Len1);
Put_Line ("Enter the second template:");
Get_Line (Template2, Len2);

declare
Result : Natural := Match_Template (Template1, Template2, Len1, Len2);
begin
if Result = Natural'Last then
Put_Line ("No matching string exists.");
else
Put_Line ("The minimum length of the matching string is: " & Natural'Image (Result));
end if;
end;
end Pattern_Matching;



huul2
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.