ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1423. String Tale

Помогите!!!Лимит времени(=
Posted by Dj_Denis 11 Nov 2009 17:42
Что здесь поменять чтоб количество циклов было меньше...

var n,x,i,j,t:integer;
a,b:array[1..250000]of char;
tempchar,ch:char;
label fin,step;
begin
readln(n);
x:=-1;
for i:=1 to n do read(a[i]);
readln;
for i:=1 to n do read(b[i]);
readln;
i:=1;
while i<=n  do begin
j:=1;
                 while  j<=n do begin
                                if j=1 then begin
                                tempchar:=a[1];
                                a[1]:=a[n];
                                            end else begin
                                            ch:=a[j];
                                            a[j]:=tempchar;
                                            tempchar:=ch;
                                            end;
                                            j:=j+1;
                                end;
                 for t:=1 to n do if a[t]=b[t] then x:=i else begin
                                                              x:=-1;goto step;
                                                              end;
                 if i=n then x:=0;
                 goto fin;
                 step:i:=i+1;
                 end;
fin:write(x);
end.
Re: Помогите!!!Лимит времени(=
Posted by AterLux 7 Sep 2010 20:43
Your realisation is 'naive', takes in worst case O(N^2) operations. for 250000 it's about 62,5 billions iterations. It will take almost 1 minute on even strong CPU's

Use, for example, hash function.

Try test with 249999 equal symbols and one another
Re: Помогите!!!Лимит времени(=
Posted by Andrew Hoffmann aka SKYDOS [Vladimir SU] 7 Sep 2010 22:34
Избежать лемит времени можно написав КМП ;)