zoukankan      html  css  js  c++  java
  • hdu1358 利用next【】数组求重复前缀

    自己用暴力做的,超了,网上搜了之后才知道还带这样滴;

    #include<iostream>
    #include <string>
    //#include <window.h>
    //#include <system>
    using namespace std;
    const int Max=1000011;
    int next[Max];
    //int flag[Max];

    string str;
    void get_next(int n)
    {
    int j=-1;
    int i=0;
    while(i<n)
    {
    if(j==-1||str[i]==str[j])
    { j++;i++;
    next[i]=j ;
    }
    else
    {
    j=next[j];
    }
    }
    }
    int main()
    {
    int NO=1;
    int n;

    while(cin>>n)
    {
    if(n==0)
    break;
    cin>>str;
    int i,j;
    memset(next,0,sizeof(next));
    next[0]=-1;
    get_next(n);

    cout<<"Test case #"<<NO++<<endl;
    for(i=2;i<=n;i++)
    {
    int temp=i%(i-next[i-1]-1);
    int temp2=i/(i-next[i-1]-1);
    if(temp==0)
    if(i-next[i-1]-1==1&&str[i-1]==str[i-2])
    cout<<i<<" "<<i<<endl;
    else
    if(i-next[i-1]-1!=1&&str[i-1]==str[(i-next[i-1]-1)-1])
    cout<<i<<" "<<i/(i-next[i-1]-1)<<endl;
    }
    cout<<endl;

    }
    return 0;
    }
  • 相关阅读:
    工具
    选择排序
    c#中queue的用法
    c#加密
    话谈c#拷贝
    const与readonly的区别
    WinForm中使MessageBox实现可以自动关闭功能
    c#winform关闭窗口时触发的事件
    委托
    C# STA和MTA线程设置
  • 原文地址:https://www.cnblogs.com/orangeblog/p/2415250.html
Copyright © 2011-2022 走看看