zoukankan      html  css  js  c++  java
  • POJ 2751:Seek the Name, Seek the Fame(Hash)

                         Seek the Name, Seek the Fame

    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 24077   Accepted: 12558

    Description

    The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm: 

    Step1. Connect the father's name and the mother's name, to a new string S. 
    Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S). 

    Example: Father='ala', Mother='la', we have S = 'ala'+'la' = 'alala'. Potential prefix-suffix strings of S are {'a', 'ala', 'alala'}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:) 

    Input

    The input contains a number of test cases. Each test case occupies a single line that contains the string S described above. 

    Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000. 

    Output

    For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby's name.

    Sample Input

    ababcababababcabab
    aaaaa
    

    Sample Output

    2 4 9 18
    1 2 3 4 5
    

    题意

    给出一个字符串ch,求出ch中存在多少子串,使得这些子串既是ch的前缀,又是ch的后缀。从小到大依次输出这些子串的长度。 

    思路

    这个题好像很多都是用KMP写的,百度看了看代码,递归的那个过程不是太理解,就用Hash写了,感觉Hash的思路更简单

    记得要提前预处理一下

    AC代码

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <math.h>
    #include <limits.h>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <set>
    #include <string>
    #define ll long long
    #define ull unsigned long long
    #define ms(a) memset(a,0,sizeof(a))
    #define pi acos(-1.0)
    #define INF 0x7f7f7f7f
    const double E=exp(1);
    const int maxn=1e6+10;
    const int mod=1e9+7;
    const int base=2333;
    using namespace std;
    char ch[maxn];
    ull a[maxn];
    ull b[maxn];
    int vis[maxn];
    int main(int argc, char const *argv[])
    {
    	a[0]=1;
    	for(int i=1;i<maxn;i++)
    		a[i]=a[i-1]*base;
    	while(cin>>(ch+1))
    	{
    		ms(b);
    		int l=strlen(ch+1);
    		b[1]=ch[1];
    		for(int i=2;i<=l;i++)
    			b[i]=b[i-1]+a[i-1]*ch[i];
    		int k=0;
    		for(int i=1;i<=l;i++)
    		{
    			if(b[i]*a[l-i]==(b[l]-b[l-i]))
    				vis[k++]=i;
    		}
    		for(int i=0;i<k;i++)
    		{
    			if(i)
    				cout<<" ";
    			cout<<vis[i];
    		}
    		cout<<endl;
    	}	
    	return 0;
    }
    
  • 相关阅读:
    OpenCV2:总结篇 PS算法实现
    DirectX9:第四部分 顶点着色器
    DirectX9:基础篇 第一章 初始化Direct3D
    OpenCV2:高中篇 图像锐化技术-边缘检测算子
    他山之石:C++书籍推荐
    C89:论回调函数
    OpenCV2:幼儿园篇 第六章 图像转换
    四维空间引发-维度空间(0-10)来自我的教授的班长
    802.11全家
    提示窗UIAlertView与UIAlertController的用法(持续更新中)
  • 原文地址:https://www.cnblogs.com/Friends-A/p/10324384.html
Copyright © 2011-2022 走看看