poj2752Seek the Name, Seek the Fame(next数组)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 24390 | Accepted: 12723 |
Description
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
Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.
Output
Sample Input
- ababcababababcabab
- aaaaa
Sample Output
- 2 4 9 18
- 1 2 3 4 5
Source
- #include<iostream>
- #include<string.h>
- #include<string>
- #include<stdlib.h>
- #include<algorithm>
- #include<stdio.h>
- #include<queue>
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef long long ll;
- typedef pair<int,int> PII;
- #define mod 1000000007
- #define pb push_back
- #define mp make_pair
- #define all(x) (x).begin(),(x).end()
- #define fi first
- #define se second
- //head
- #define MAX 400005
- int next[MAX];
- char s[MAX];
- int len;
- void get_next()
- {
- int i=,j=-;
- next[i]=j;
- for(i=;i<len;i++)
- {
- while(j>-&&s[j+]!=s[i])
- j=next[j];
- if(s[j+]==s[i]) j++;
- next[i]=j;
- }
- }
- void output(int x)
- {
- if(next[x]==-)
- {
- printf("%d ",x+);
- return ;
- }
- output(next[x]);
- printf("%d ",x+);
- //cout<<"ok"<<endl;
- }
- int main()
- {
- /*ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);*/
- while(~scanf("%s",s))
- {
- memset(next,,sizeof(next));
- len=strlen(s);
- get_next();
- /*for(int i=0;i<len;i++)
- cout<<next[i]<<" ";
- cout<<endl;*/
- output(len-);
- printf("\n");
- }
- return ;
- }
poj2752Seek the Name, Seek the Fame(next数组)的更多相关文章
- poj2752seek the name, seek the fame【kmp】
The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the l ...
- poj2752Seek the Name, Seek the Fame
Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...
- POJ 2752Seek the Name, Seek the Fame(next数组妙用 + 既是前缀也是后缀)
题目链接 题意:求一个字符串中 前缀 和 后缀 相同的长度 分析: 对于一个字符串他自己的长度肯定是可以的.然后如果满足 前缀 和 后缀相等,那个前缀 最后一个字符 一定 和 该字符串最后一个字符相等 ...
- POJ 2752 Seek the Name, Seek the Fame(next数组的理解)
做此题,只要理解好next数组就行....................... #include <cstdio> #include <cmath> #include < ...
- POJ--2752--Seek the Name, Seek the Fame【KMP】
链接:http://poj.org/problem? id=2752 题意:对于一个字符串S,可能存在前n个字符等于后n个字符,从小到大输出这些n值. 思路:这道题加深了对next数组的理解.next ...
- POJ 2752 Seek the Name, Seek the Fame next数组理解加深
题意:给你一个字符串,寻找前缀和后缀相同的子串(包括原串). 从小到大排列输出其子串的长度 思路:KMP next 数组应用. 其实就是一个数学推导过程. 首先由next数组 可知s(ab) = s ...
- poj2752 Seek the Name, Seek the Fame(next数组的运用)
题目链接:id=2752" style="color:rgb(202,0,0); text-decoration:none; font-family:Arial; font-siz ...
- POJ 2752 Seek the Name, Seek the Fame(next数组运用)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24000 ...
- POJ2752 Seek the Name, Seek the Fame —— KMP next数组
题目链接:https://vjudge.net/problem/POJ-2752 Seek the Name, Seek the Fame Time Limit: 2000MS Memory Li ...
随机推荐
- zabbix 微信告警脚本
#!/usr/bin/env python3 import requests import json import sys import os def access_token(Corpid,Secr ...
- Linux筛选数据
grep命令:在文件中查找关键字,并显示所在行(Globally search a Regular Expression and Print) grep text file:在file文件中搜索tex ...
- Center os 用户环境变量
vi ~/.bash_profile进入用户环境变量设置 export JAVA_HOME=/usr/java/jdk1.7.0_76export JAVA_BIN=$JAVA_HOME/binexp ...
- 【串线篇】SQL映射文件-resultMap自定义封装
mybatis默认封装规则: 1).按照列明和属性名一一对应的规则(不区分大小写) 2).如果不一一对应: 1).开启驼峰命名(数据库aaa_bbb, 程序中aaaBbb) 2).起别名 3).自定义 ...
- SPOJ1693 COCONUTS - Coconuts
传送门[洛谷] 自闭QAQ 什么玩意QAQ 不是很理解到底在干啥 问了巨佬以后大概是这个样子的 可以看出是最小割模型 对于每一个人 反悔的话就是代价+1 那么连接(s,i) (i,t)分别表示他最后选 ...
- 使用字符流(Writer、Reader)完成对文件的读写操作
字符流 字符输出流:Writer,对文件的操作使用子类FileWriter 字符输入流:Reader,对文件的操作使用子类FileReader 每次操作的是一个字符 文件字符操作流会自带缓存,默认大小 ...
- setInterval,setTimeout,clearInterval
定时器 var i=0; function iadd(){ i++; console.log(i) } setInterval(iadd,1000);//1.2.3... 超时调用 var i=0; ...
- zoj 3325 Machine(线段树)
题意:0~n-1的数组,初始值为0:执行m个操作,每次操作执行后输出当前值为0的连续段的段数. 操作1: p i j : i~j区间的每个元素值减1 操作2: r i j :i~j区间的每个元素值加1 ...
- JS 判断undefined
tax !== underfined underfined 是判断的是类型的结果, 如果加typeof后是字符串类型 写法:typeof(tax) !== "underfined" ...
- auto_now_add与auto_now的区别