↓ 跳过正文
  1. Posts/

poj 3974 Palindrome (最长回文字串,manacher裸题)

·297 字·1 分钟

poj3974 题意:求最大长度的回文字串。 思路:manacher裸题,用来练习算法。

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年04月18日 星期一 16时32分25秒
 4File Name :code/poj/3974.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=2E6+7;
34char st[2*N];
35int p[2*N];
36
37int manacher(char *s)
38{
39    int len = strlen(s);
40    for ( int i = len ; i >= 0 ; i-- )
41    {
42	s[i+i+2]=s[i];
43	s[i+i+1]='#';
44    }
45    s[0]='$';
46
47    int id = 0 ;
48    int maxlen = 0 ;
49
50    for ( int i = 2 ; i < 2*len+1 ; i++)
51    {
52	if (id+p[id]>i) p[i] = min(p[2*id-i],id+p[id]-i);
53	else p[i] = 1;
54
55	while (s[i+p[i]]==s[i-p[i]]) p[i]++;
56	if (id+p[id]<i+p[i]) id =  i;
57	if (p[i]>maxlen) maxlen = p[i];
58    }
59    return maxlen-1;
60}
61int main()
62{
63	#ifndef  ONLINE_JUDGE
64	freopen("code/in.txt","r",stdin);
65  #endif
66	int cas = 0 ;
67	while (scanf("%s",st)!=EOF)
68	{
69	    if (st[0]=='E'&&st[1]=='N'&&st[2]=='D') break;
70	    printf("Case %d: %d\n",++cas,manacher(st));
71
72	}
73
74  #ifndef ONLINE_JUDGE
75  fclose(stdin);
76  #endif
77    return 0;
78}

相关文章

BZOJ 1657: [Usaco2006 Mar]Mooo 奶牛的歌声 (单调栈)

·1062 字·3 分钟
1657: [Usaco2006 Mar]Mooo 奶牛的歌声 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 634 Solved: 447 [Submit][Status][Discuss] Description # Farmer John’s N (1 <= N <= 50,000) cows are standing in a very straight row and mooing. Each cow has a unique height h in the range 1..2,000,000,000 nanometers (FJ really is a stickler for precision). Each cow moos at some volume v in the range 1..10,000. This “moo” travels across the row of cows in both directions (except for the end cows, obviously). Curiously, it is heard only by the closest cow in each direction whose height is strictly larger than that of the mooing cow (so each moo will be heard by 0, 1 or 2 other cows, depending on not whether or taller cows exist to the mooing cow’s right or left). The total moo volume heard by given cow is the sum of all the moo volumes v for all cows whose mooing reaches the cow. Since some (presumably taller) cows might be subjected to a very large moo volume, FJ wants to buy earmuffs for the cow whose hearing is most threatened. Please compute the loudest moo volume heard by any cow.

BZOJ 1656: [Usaco2006 Jan] The Grove 树木(神奇的bfs之射线法)

·1177 字·3 分钟
1656: [Usaco2006 Jan] The Grove 树木 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 143 Solved: 88 [Submit][Status][Discuss] Description # The pasture contains a small, contiguous grove of trees that has no ‘holes’ in the middle of the it. Bessie wonders: how far is it to walk around that grove and get back to my starting position? She’s just sure there is a way to do it by going from her start location to successive locations by walking horizontally, vertically, or diagonally and counting each move as a single step. Just looking at it, she doesn’t think you could pass ’through’ the grove on a tricky diagonal. Your job is to calculate the minimum number of steps she must take. Happily, Bessie lives on a simple world where the pasture is represented by a grid with R rows and C columns (1 <= R <= 50, 1 <= C <= 50). Here’s a typical example where ‘.’ is pasture (which Bessie may traverse), ‘X’ is the grove of trees, ‘’ represents Bessie’s start and end position, and ‘+’ marks one shortest path she can walk to circumnavigate the grove (i.e., the answer): …+… ..+X+.. .+XXX+. ..+XXX+ ..+X..+ …+++ The path shown is not the only possible shortest path; Bessie might have taken a diagonal step from her start position and achieved a similar length solution. Bessie is happy that she’s starting ‘outside’ the grove instead of in a sort of ‘harbor’ that could complicate finding the best path.

BZOJ 3407: [Usaco2009 Oct]Bessie's Weight Problem 贝茜的体重问题(01背包)

·758 字·2 分钟
3407: [Usaco2009 Oct]Bessie’s Weight Problem 贝茜的体重问题 # Time Limit: 3 Sec Memory Limit: 128 MB Submit: 88 Solved: 79 [Submit][Status][Discuss] Description # 贝茜像她的诸多姊妹一样,因为从约翰的草地吃了太多美味的草而长出了太多的赘肉.所以约翰将她置于一个及其严格的节食计划之中.她每天不能吃多过H(5≤日≤45000)公斤的干草.贝茜只能吃一整捆干草;当她开始吃一捆干草的之后就再也停不下来了.她有一个完整