even if the left over string length is >1 so that the recursion can be fun(s,current_position-2), u still have the option for choosing a single character... do u get it?? thats where u go wrong... :) the rec call should be "return fun(cur_length-1)+fun(cur_len-2)" ...
On Wed, Jun 1, 2011 at 3:34 PM, arun kumar <kumar0...@gmail.com> wrote: > hey me getting wrong ans..can anyone pls help me out > here s my code > #include<stdio.h> > #include<iostream> > #include<string> > #include<cstring> > using namespace std; > unsigned long long a[5001]={0}; > unsigned long long fun(string &s,int n) > { > if(n==0) return 1; > if(a[n]) return a[n]; > > int c=0,d=0; > c=s[n]-'0'; > d=s[n-1]-'0'; > unsigned long long &ans=a[n]; > // cout<<c<<d<<endl; > if((d*10+c)<=26 && n!=1) > ans+=fun(s,n-2); > else if((d*10+c)<=26) > ans+=fun(s,0); > if(d!=0) > ans+=fun(s,n-1); > return ans; > > > } > main() > { > string s; > while(cin>>s && s[0]!='0') > { > memset(a,0,sizeof(a)); > cout<<fun(s,s.size()-1); > > } > } > > > On Wed, Jun 1, 2011 at 3:30 PM, pacific :-) <pacific4...@gmail.com> wrote: > > Link : https://www.spoj.pl/problems/ACODE/ > > > > 25114 > > BEAN’, ‘BEAAD’, ‘YAAD’, ‘YAN’, ‘YKD’ ‘BEKD’. > > How many different decodings?” > > > > My soln , but i get TLE.Please help. > > > > #include <iostream> > > #include <cstdio> > > #include <vector> > > using namespace std; > > > > char * head; > > int result[5001]; > > int count(char * a ,int size) > > { > > if(result[a-head]!=0){ > > return result[a-head]; > > } > > > > if(size==1) > > return 1; > > else if (size==2) > > { > > if(a[0]>'2') > > return 1; > > else > > return 2; > > } > > else > > { > > int temp = count(a+1,size-1); > > if(a[0]>'2' || (a[0]=='2' && a[1]>'6')) > > { > > result[a-head] = temp ; > > return temp; > > } > > else > > { > > int r = temp+count(a+2,size-2); > > result[a-head] = r; > > return r; > > } > > } > > } > > > > int main() > > { > > char ch; > > cin>>ch; > > while(ch!='0') > > { > > char input[5001]; > > int index=0; > > while(ch!='\n') > > { > > //input.push_back(ch-'0'); > > input[index]=ch; > > index++; > > scanf("%c",&ch); > > } > > > > cin>>ch; > > head = input ; > > for(int i=0;i<=5000;i++) > > result[i]=0; > > cout<<count(input,index)<<endl; > > } > > return 0; > > } > > > > -- > > regards, > > chinna. > > > > -- > > You received this message because you are subscribed to the Google Groups > > "Algorithm Geeks" group. > > To post to this group, send email to algogeeks@googlegroups.com. > > To unsubscribe from this group, send email to > > algogeeks+unsubscr...@googlegroups.com. > > For more options, visit this group at > > http://groups.google.com/group/algogeeks?hl=en. > > > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.