My solution is 20 lines. Easy problem. 14 lines solution with using recursive function. I have recursive function too. Whatever, Good Job! 7 lines with recursive func in python 3. Or 14 with normal look a code. #pragma comment(linker, "/STACK:16777216") #include <stdio.h> #include <string.h> #define size 20010 char str[size]; char arr[size]; int i=0; void recurse(int low,int high){ int mid; if(low>high) return; mid = (low+high)/2; arr[mid] = str[i++]; recurse(low,mid-1); recurse(mid+1,high); } int main(){ scanf(" %[^\n]s",str); recurse(0,strlen(str)-1); printf("%s\n",arr); return 0; } /*I simply used recursion and its working for all the test I know presently.Please tell me what is the problem in the code*/ the same def solve(str): l = len(str) if l<=2: return str mid = (1+l)//2 return solve(str[1:mid]) + str[0] + solve(str[mid:]) print solve(sys.stdin.readline().strip()) it's working for all the test I know, but get WA3 here Xel,отличное решение! Только вместо функции strip() нужна функция strip('\n'), иначе удаляются все концевые пробелы и этим самым нарушаются входные данные. Edited by author 03.02.2019 13:56 My solution in Python 3 took 0.078 seconds and 516 KB with recursion. Can you do it faster? you can try to to it with stack instead of recursion Не понимаю фразу:"Строка должна оканчиваться символом перевода строки." Что это интересно за символ в паскале? Мой код: const Nmax=21000; var a,s:array[1..Nmax] of char; N,i,L,R:longint; ch:char;
procedure Solve(L,R,i:longint); var m:longint; begin if(R-L>1) then begin m:=(L+R) div 2; a[m]:=s[i]; Solve(L,m-1,i+1); Solve(m+1,R,m-L+i+1); end else begin a[l]:=s[i]; if(R>L) then a[R]:=s[i+1]; end; end;
begin N:=0; while not eof do begin read(ch); inc(N); s[n]:=ch; end;
L:=1; R:=N; Solve(L,R,1); for i:=1 to N-1 do write(a[i]); writeln(a[n]); end. Возможно, нужно вывести все символы в строке с помощью команды write, а затем просто добавить строку writeln. Test 15 is a space or multispace!! Be careful!! Test : abcdefgh Answer : dbacfeg Is this answer correct? no, correct answer is "cbdafegh" Does anybody has any idea? Me too and I dont think I'm wrong Send your email. I help you. Test : abcdefgh Answer : dbacfeg Is this answer correct? нет! как минимум, в первом слове букв больше :D Edited by author 01.05.2012 15:15 Edited by author 01.05.2012 15:15 Oh, of course :D I want to say, is answer dbacfegh is correct. But now I already know, that my old algorithm hadn't been correct and here is just recursion. What is "г рковоаззи- аул лаывгвдяыеат"? I have no desire to suffer with Russian letters in the console. Answer: "корова из-за угла выглядывает". Translated by Google: "cow from behind a corner peeps". Sorry for my bad English :) #include <iostream> #include <string> #include <windows.h> using namespace std; string s; string f( long b, long e ){ //lalalalalala } int main(){ #ifndef ONLINE_JUDGE freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); #endif SetConsoleCP(1251); SetConsoleOutputCP(1251); s = "г рковоаззи- аул лаывгвдяыеат"; cout << f( 0, s.size()-1 ) << endl; return 0; } where i am wrong please write me aramvmv@mail.ru #include <iostream> using namespace std; __int64 i,n,k, o[20110]={0},l=0, s,t,j,m,r; char a[20110], c[20110]; int main() { cin.ignore(); cin.getline(a,20110); n=strlen(a); k=n; t=1; while(k!=0) {c[(k+1)/2]=a[t]; o[(k+1)/2]=1; k=(k+1)/2; t++; } for(j=0;j<n/4;j++) { for(i=1;i<=n;i++) if(o[i]==0) {l=1; m=s=i; while(o[i+l]==0) {l++; m+=i+l;} if(l==0) break; if(l==1) {c[s]=a[t]; o[s]=1;} else {k=m/l; while(k>=s) {c[k]=a[t]; o[(k+1)/2]=1; l/=2;m=0; for(r=s;r<s+l;r++)m+=r; k=m/l; t++; }}}} for(i=1;i<=n;i++) cout<<c[i]; return 0;} no crash here is my new solution but again crash #include <iostream> using namespace std; __int64 i,n,k, o[20110]={0},l=0, s,t,j,m,r; char a[20110], c[20110]; int main() { cin.ignore(); cin.getline(a,20110); n=strlen(a); k=n; t=1; while(k!=0) {c[(k+1)/2]=a[t]; o[(k+1)/2]=1; k=(k+1)/2; t++; } for(j=0;j<n/4;j++) { for(i=1;i<=n;i++) if(o[i]==0) {l=1; s=i; while(o[i+l]==0) {l++; } if(l==0) break; if(l==1) {c[s]=a[t]; o[s]=1;} else {k=s+l; while(k>=s) {c[(k+s)/2]=a[t]; o[(k+s)/2]=1; k=(k+s)/2; t++; }}l=0; }} for(i=1;i<=n;i++) cout<<c[i]; return 0;} I solved this problem by Java with recursive function. I think it's more easy :) recursive function is the best method for solving this problem, i thenk =D My program passes tests "kyaVs alisse uop." and "gtorhoprahy" but get WA1. Please help. Now AC. The problew was that I used getchar() instead of gets(). With array of 20001 chars (with '\0') I got wa17, but of 20002 I got AC. So the input string can contain 20001 chars, can't it? And in the statement has been said "The text is no longer than 20000"... please smb send me the solution or the idea my mail is vgvavlad@gmail.com [code deleted] Edited by moderator 19.11.2019 23:23 Thank you very much for this algoritm! This is exellent program! Mods, send input data for test1 please. Trying to solve the problem second day already. Thx vanvector@mail.ru If using Java BufferedReader pay attention on number of symbols read. While testing in eclipse I used N-1. But on Timus server N-2 was accepted Edited by author 06.10.2009 16:13 orthography gives gtorhoprahy but “one eye, one horn, but not a rhino” gives “reo ac wpeosr tu fdbmo heinoa cnrer” where are the comma's gone??? somebody please help me understand the problem Edited by author 10.08.2009 12:02 Edited by author 10.08.2009 12:02 Edited by author 10.08.2009 12:02 Why Crash (stack overflow)#8 ???? Help,please =) Use directive somelike #pragma comment(linker, "/STACK:16777216") This is described in the FAQ to this site. |
|