标签:des style class blog code java
15 1 12 123 1234 12345 9 98 987 9876 98765 89 32 51075176167176176176 347746739 5610
Case #1: 0 Case #2: 25 Case #3: 226 Case #4: 1628 Case #5: 49516 Case #6: 15 Case #7: 15 Case #8: 15 Case #9: 43764 Case #10: 49750 Case #11: 10 Case #12: 51 Case #13: -1 Case #14: 1233 Case #15: 22374
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 7000000;
const int INF = 1e8;
int ch[maxn][10];
int val[maxn];
int cnt;
char c[200];
char str[200];
void add(char a[],char b[],char back[]){
int i=strlen(a)-1,j=strlen(b)-1,k=0;
int x,y,z;
int up=0;
while(i>=0||j>=0)
{
if(i<0)x=0;
else x=a[i]-'0';
if(j<0)y=0;
else y=b[j]-'0';
z=x+y+up;
c[k++]=z%10+'0';
up=z/10;
i--;
j--;
}
if(up>0)c[k++]=up+'0';
for(i=0;i<k;i++)back[i]=c[k-1-i];
back[k]='\0';
}
int getIdx(char a){
return a-'0';
}
void insert(char st[],int d){
int u = 0;
for(int i = 0; i < strlen(st) && i < 42; i++){
int k = getIdx(st[i]);
if(!ch[u][k]){
val[cnt] = d;
ch[u][k] = cnt++;
memset(ch[cnt],0,sizeof ch[cnt]);
}
u = ch[u][k];
}
}
int query(char st[]){
int u = 0;
for(int i = 0; i < strlen(st); i++){
int k = getIdx(st[i]);
if(!ch[u][k]){
return -1;
}
u = ch[u][k];
}
return val[u];
}
void init(){
cnt = 1;
memset(ch[0],0,sizeof ch[0]);
for(int i = 0; i < maxn; i++)
val[i] = INF;
char a[200],b[200],ans[200];
a[0] = '1',a[1] = 0;
b[0] = '1',b[1] = 0;
insert(a,0);
for(int i = 2; i < 100000; i++){
if(strlen(b) > 70){
a[strlen(a)-1] = 0;
b[strlen(b)-1] = 0;
}
add(a,b,ans);
insert(ans,i);
strcpy(a,b);
strcpy(b,ans);
}
}
int main(){
init();
int ncase,T=1;
cin >> ncase;
while(ncase--){
cin >> str;
printf("Case #%d: %d\n",T++,query(str));
}
return 0;
}
HDU4099-Revenge of Fibonacci(trie树+数学基础),布布扣,bubuko.com
HDU4099-Revenge of Fibonacci(trie树+数学基础)
标签:des style class blog code java
原文地址:http://blog.csdn.net/mowayao/article/details/32718675