We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I believe my algorithm below should be correct and it works for cases 1-21. But it is not optimized since it reports "time limit exceeded" for cases beyond 22... Any thoughts about it? Thanks!
voidfindInd(vector<longlong>&fib,longlong&n,int&j){//Note that fib[j] = fib[j-2] + fib[j-1]//We need to confirm whether n is within fib[j-2] or fib[j-1]if(j==0||j==1){return;}if(n>fib[j-2]){//n is beyond fib[j-2], so we chop off fib[j-2] from n//And look for new n within fib[j-1]n-=fib[j-2];j--;findInd(fib,n,j);}else{//n is within fib[j-2] j=j-2;findInd(fib,n,j);}}charfindChar(stringsa,stringsb,longlongn){longlongla=sa.length();longlonglb=sb.length();if(n<=la){return(sa[n-1]);}if(n<=la+lb){stringsc=sa+sb;return(sc[n-1]);}vector<longlong>fib;fib.push_back(la);fib.push_back(lb);while(fib[fib.size()-1]<n){fib.push_back(fib[fib.size()-2]+fib[fib.size()-1]);}intj=fib.size()-1;findInd(fib,n,j);if(j==0){returnsa[n-1];}else{returnsb[n-1];}}intmain(){/* Enter your code here. Read input from STDIN. Print output to STDOUT */intq;cin>>q;for(inti=1;i<=q;i++){stringsa,sb;longlongn;cin>>sa>>sb>>n;cout<<findChar(sa,sb,n)<<endl;}return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #230: Fibonacci Words
You are viewing a single comment's thread. Return to all comments →
I believe my algorithm below should be correct and it works for cases 1-21. But it is not optimized since it reports "time limit exceeded" for cases beyond 22... Any thoughts about it? Thanks!