Решение №7 из главы 16.Проверить можно ли строки из m перестановкой символов получить строку n
Primary tabs
Проверить можно ли строки из m перестановкой символов получить строку n.
Примеры (входные данные и ответ):1 abc и bca-- можно
1 abc и baa-- нельзя
1 abcc и cbac-- можно
1 abc4c и cb55ac-- нельзя
program compar_two_string; function symb_count( symb: string; str2: string ): integer; var count,i,j : integer; begin count := 0; for i:= 1 to length(symb) do begin for j:= 1 to length(str2) do begin if symb = str2[j] then count += 1; end; end; symb_count := count; // кол- во вхождений символа end; function comparison_two_string(str1: string; str2: string): boolean; var count1, count2, res, i : integer; begin count1 := 0; count2 := 0; res := 0; for i:= 1 to length(str1) do // обход первой строки begin count1 := symb_count( str1[i], str2 ); count2 := symb_count( str1[i], str1 ); if( count1 = count2 ) then res += 1; count2 := 0; // обнуляем счетчик end; if (res = length(str1)) then comparison_two_string:= True else comparison_two_string:= False; end; var str1, str2 : string; fl : boolean; begin // основная программа fl := True; while fl do begin writeln('Input two string:'); readln(str1); readln(str2); { при разной длине будет работать неправильно } if(length(str1)) = (length(str2)) then // вызов функции if(comparison_two_string(str1,str2)) or not (comparison_two_string(str1,str2)) then begin writeln(comparison_two_string(str1,str2)); fl := False; end; end; end.
- Log in to post comments
- 733 reads