前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >UVA12467「Secret Word」

UVA12467「Secret Word」

作者头像
hotarugali
发布2022-03-02 20:29:34
6980
发布2022-03-02 20:29:34
举报
文章被收录于专栏:hotarugaliの技术分享

1. 题目

题目链接:UVA12467「Secret Word」

Description

Alicia and Roberto like to play games. Today, Roberto is trying to guess a secret word that Alicia chose. Alicia wrote a long string Sin a piece of paper and gave Roberto the following clues:

  • The secret word is a non-empty substring (A substring of SSS is defined as any consecutive sequence of characters belonging to S. For example, if S = abcd then a, abcd, ab, bc and bcd are some of the substrings of S but ac, aa, aabbccdd and dcba are not substrings of S) of S (possibly equal to S).
  • S starts with the secret word reversed.

Roberto knows Alicia very well, and he’s sure that if there are several possible secret words that satisfy the clues above, Alicia must have chosen the longest one.

Can you help him guess the secret word?

Input

The first line of the input file contains a single integer number T \leq 150, the number of test cases.

T lines follow, each with a single stringS. S will only contain lowercase English letters. The length of Swill not exceed one million characters.

Output

For each test case, output the secret word in one line.

Explanation of the sample cases:

  • colombia: if you take c and reverse it you get c. colombia starts with c, so this satisfies the two clues above. Furthermore, c is the longest word that satisfies the two clues so it must be the secret word.
  • abcdba: if you take ba and reverse it you get ab. abcdba starts with ab and there’s no longer substring that satisfies the two clues.
  • neversayeven: if you take even and reverse it you get neve. neversayeven starts with neve and there’s no longer substring that satisfies the two clues.
  • neveroddoreven: this case is a palindrome so if we reverse it we get the same string. neveroddoreven starts with neveroddoreven (since they are the same) and obviously there’s no longer substring that satisfies that, so this is the secret word.
  • listentothesilence: Notice the secret word might be somewhere in the middle of the big word.

Sample Input

代码语言:javascript
复制
5
colombia
abcdba
neversayeven
neveroddoreven
listentothesilence

Sample Output

代码语言:javascript
复制
c
ba
even
neveroddoreven
sil

2. 题目

2.1 分析

  • 由题意可知,最终的 secret 满足 secret 和 secret 的反转 secret’ 均在字符串 s中。将字符串 s 反转后的得到 s′,然后将 s s′拼接在一起得到S = s + \# + s',在对 S求前缀函数。然后统计 \pi[n+1..2n] 中最大的,此即为满足题意的最长 secret 的长度,设对应下标为 pos
  • 由于 ss′ 关于下标x = n对称,故最终答案为s[2n-pos..2n-pos+\pi[pos]-1]

2.2 代码

代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;

// 前缀函数
struct PrefixFunction {
    #ifndef _PREFIXFUNCTION_
    #define ll int
    #define MAXN 2000005
    #endif
    ll pi[MAXN];        // 前缀函数
    PrefixFunction() {}
    // 计算前缀函数
    void getPi(char *str, ll n) {
        pi[0] = 0;
        ll i = 1, j = pi[i-1];
        while(i < n) {
            if(str[i] == str[j]) {
                pi[i++] = j++ + 1;
            } else if(!j) {
                pi[i++] = j;
            } else {
                j = pi[j-1];
            }
        }
    }
};

int main()
{
    static char str[MAXN];
    static PrefixFunction pf;
    ll t;
    scanf("%d", &t);
    for(ll k = 0; k < t; ++k) {
        scanf("%s", str);
        ll n = strlen(str);
        for(ll i = 0, j = n-1; ~j; --j,++i) {
            str[n+1+i] = str[j];
        }
        pf.getPi(str, 2*n+1);
        ll ans = 0, pos;
        for(ll i = n+1; i < 2*n+1; ++i) {
            if(pf.pi[i] > ans) {
                ans = pf.pi[i];
                pos = i;
            }
        }
        str[2*n-pos+ans] = '\0';
        printf("%s\n", &str[2*n-pos]);
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 题目
    • Description
      • Input
        • Output
          • Sample Input
            • Sample Output
            • 2. 题目
              • 2.1 分析
                • 2.2 代码
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档