zoukankan      html  css  js  c++  java
  • 53.FIB词链


    时间限制: 1 s

     空间限制: 128000 KB

     题目等级 : 大师 Master

    题解

    题目描述 Description

    Fibonacci词定义如下:FIB1  FIB2 >= 1FIBk+2 FIBk+1*FIBk 
    其中表示词的连接。因此,我们有:

     FIB3 ab;     FIB4 aba;    FIB5 abaab;     FIB6 abaababa.

    问题是,对给定的词,问该词在FIBn中出现多少次。 

    输入描述 Input Description

    输入有两行,第一行为给出的只含有 b的词,长度不超过30.第二行给出一个正整数n <= 200.

    输出描述 Output Description

    对给定的词,输出在FIBn中出现的次数 

    样例输入 Sample Input

    aba

    样例输出 Sample Output

    3

    错误代码:

    #include

    using namespace std;

    #include

    #include

    char fib[201][1000000],ch[31],n,sum=0;

    void input();

    void search();

    int main()

    {

           input();

           search();

           printf("%d",sum);

           return 0;

    }

    void search()

    {

           int chlong=strlen(ch),fiblong=strlen(fib[n]);

           int fibl=0,chl=0;

           while(fibl

           {

                  int cpyfibl=fibl;

                  while(fib[n][cpyfibl]==ch[chl]&&chl

                  {

                         chl++;

                         cpyfibl++;

                  }

                  if(chl==chlong)

                  {

                         sum++;

                         chl=0;

                  }

                  if(chl!=chlong)

                  {

                         chl=0;

                  }

                  fibl++;

           }

     

    }

    void input()

    {

           scanf("%s",ch);

           scanf("%d",&n);

           fib[1][0]='b';

           fib[2][0]='a';

           for(int i=3;i<=n;++i)

           {

                  strcpy(fib[i],fib[i-1]);

                  strcat(fib[i],fib[i-2]);

           }

    }

    记录第i个词的前M-1位和后M-1位,在递推的时候合并并且更新答案就可以了。

    其中M表示所求字符串的长度。

    特判:如果M=0的话直接上Fib数。

    注意要高精,压四位很好写吧,那个20的常数是高精带来的。

  • 相关阅读:
    Python里的目录方法
    PythonFile对象的属性
    Python read和write方法
    Python打开和关闭文件
    Python打印到屏幕_读取键盘输入
    Python包
    Python globals和locals函数_reload函数
    Python dir( )函数
    【C 语言】一元二次方程
    【C语言】已知三角形三边长,求三角形面积
  • 原文地址:https://www.cnblogs.com/csgc0131123/p/5290375.html
Copyright © 2011-2022 走看看