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;
    }
    
  • 相关阅读:
    useState 的介绍和多状态声明(二)
    PHP:相对于C#,PHP中的个性化语法
    PHP:IIS下的PHP开发环境搭建
    PHP:同一件事,有太多的方式
    Javascript:再论Javascript的单线程机制 之 DOM渲染时机
    Javascript:拦截所有AJAX调用,重点处理服务器异常
    DDD:谈谈数据模型、领域模型、视图模型和命令模型
    .NET:再论异常处理,一个真实的故事
    Javascript:由 “鸭子类型” 得出来的推论
    Workflow:采用坐标变换(移动和旋转)画箭头
  • 原文地址:https://www.cnblogs.com/Friends-A/p/10324384.html
Copyright © 2011-2022 走看看