标签:ota ring arch about its cep cci nts deb
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 204800/204800 K (Java/Others)
Total Submission(s): 3218 Accepted Submission(s): 821
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; #define ql(a) memset(a,0,sizeof(a)) #define LL long long const int UP=50; const int N=100000-1; struct node { int val; node *child[10]; node(){val=-1;for(int i=0;i<10;++i) child[i]=NULL;} }*root; void ins(char *s,int num) { node *p=root; int minn=min(40,(int)strlen(s)); for(int i=0;i<minn;++i){ int t=s[i]-‘0‘; if(p->child[t]==NULL){ p->child[t]=new node(); } p=p->child[t]; if(p->val<0) p->val=num; } } void init() { int f1[65],f2[65],f3[65],r=0; ql(f1),ql(f2),ql(f3); ins("1",0); f1[0]=f1[1]=f2[0]=f2[1]=1; for(int i=2;i<=N;++i){ql(f3);r=0; int ml=max(f1[0],f2[0]); for(int j=1;j<=ml;j++){ f3[j]=f1[j]+f2[j]+r; r=f3[j]/10; f3[j]%=10; if(j==ml&&r) ml++; }f3[0]=ml; char s[65]; ql(s);int l=0; for(int j=f3[0];j>=1;j--) s[l++]=f3[j]+‘0‘; for(int j=41;j<=f3[0];j++) s[j-1]=‘\0‘; ins(s,i); ql(f1); for(int j=0;j<=f2[0];j++) f1[j]=f2[j]; ql(f2); for(int j=0;j<=f3[0];j++) f2[j]=f3[j]; if(ml>55){ for(int j=1;j<f1[0];j++) f1[j]=f1[j+1]; f1[f1[0]--]=0; for(int j=1;j<f2[0];j++) f2[j]=f2[j+1]; f2[f2[0]--]=0; } } } int Find(char *s) { int len=strlen(s); if(!strcmp(s,"1")) {return 0;} node *p=root; for(int i=0;i<len;++i){ int t=s[i]-‘0‘; if(p->child[t]==NULL) return -1; p=p->child[t]; } return p->val; } int main() { int k,cas=0; char p[55]; root=new node(); init(); cin>>k; while(k--){ scanf("%s",p); printf("Case #%d: %d\n",++cas,Find(p)); } return 0; }
标签:ota ring arch about its cep cci nts deb
原文地址:http://www.cnblogs.com/zzqc/p/7231697.html