POJ 3974 马拉车

POJ 3974

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
#define ll              long long
#define pii             pair<int, int>
#define rep(i,a,b)      for(ll  i=a;i<=b;i++)
#define dec(i,a,b)      for(ll  i=a;i>=b;i--)
#define forn(i, n)      for(ll i = 0; i < int(n); i++)
using namespace std;
int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846;
const double eps = 1e-6;
const int mod = 1e7 + 7;

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
inline ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
void exgcd(ll A, ll B, ll& x, ll& y)
{
    if (B) exgcd(B, A % B, y, x), y -= A / B * x; else x = 1, y = 0;
}

bool prime(int x) {
    if (x < 2) return false;
    for (int i = 2; i * i <= x; ++i) {
        if (x % i == 0) return false;
    }
    return true;
}
inline int qpow(int x, ll n) {
    int r = 1;
    while (n > 0) {
        if (n & 1) r = 1ll * r * x % mod;
        n >>= 1; x = 1ll * x * x % mod;
    }
    return r;
}

string s;
int cas;
void manacher()
{
    vector<char> ss(2 * s.size() + 1, '#');
    vector<int> p(ss.size() + 1);
    for (int i = 0; i < ss.size(); i++)
    {
        if (i % 2 == 0)
            ss[i] = s[i / 2];
    }
    ss.insert(ss.begin(), '#');
    int ans = 0, id = 0, mx = -1;
    for (int i = 0; i < ss.size(); i++)
    {
        if (id + mx > i)
            p[i] = min(p[id * 2 - i], id + mx - i);
        while (i - p[i] - 1 >= 0 && i + p[i] + 1 <= ss.size() && ss[i - p[i] - 1] == ss[i + p[i] + 1])
            p[i]++;
        if (id + mx < i + p[i])
            id = i, mx = p[i];
        ans = max(ans, p[i]);
    }
    printf("Case %d: %d\n", ++cas, ans);
}

int main()
{

    while (cin >> s)
    {
        if (s == "END")
            break;
        manacher();
    }
    return 0;
}

 

posted @ 2020-07-21 10:53  DeaL57  阅读(139)  评论(0编辑  收藏  举报