UVA 12467 - Secret Word

UVA 12467 - Secret Word

kmp+二分,二分枚举答案的长度,用kmp判断是否合法

#include <bits/stdc++.h>

using namespace std;
#define rint register int
#define rll register long long
#define long long ll
const int maxn = 1e6 + 100;
char a[maxn], b[maxn];
int pi[maxn], f[maxn];

bool check(int mid, int n) {
    pi[mid] = 0;
    for (rint i = mid + 1, j = 0; i <= n; i++) {
        while (j > 0 && a[i] != a[mid + j]) j = pi[j + mid - 1];
        if (a[i] == a[mid + j]) j++;
        pi[i] = j;
    }
    for (int i = 1, j = 0; i <= n; i++) {
        while (j > 0 && (j == n || b[i] != a[j + mid])) j = pi[j + mid - 1];
        if (b[i] == a[j + mid]) j++;
        f[i] = j;
        if (f[i] == n - mid + 1) return true;
    }
    return false;
}

int main() {
//    freopen("in.txt", "r", stdin);
    rint _;
    scanf("%d", &_);
    while (_--) {
        scanf("%s", b + 1);
        rint n = strlen(b + 1);
        rint tot = 0;
        for (int i = n; i >= 1; i--) {
            a[++tot] = b[i];
        }
        a[n + 1] = '\0';
        rint l = 1, r = n, mid;
        while (l < r) {
            mid = (l + r) >> 1;
            if (check(mid, n)) r = mid;
            else l = mid + 1;
        }
        for (rint i = l; i <= n; i++) {
            printf("%c", a[i]);
        }
        printf("\n");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/albert-biu/p/11313742.html

时间: 2024-10-14 23:11:54

UVA 12467 - Secret Word的相关文章

UVA - 621 Secret Research(水)

开始 没看懂最后一句话,后来 发现不用去考虑不能匹配的情况,那么就是一个水题了. #include<cstdio> #include<cstring> char s[10000]; int main() { int n,i,j; scanf("%d",&n); getchar(); for(i=0;i<n;i++) { scanf("%s",s); int l=strlen(s); if(strcmp(s,"1&quo

843: Guess the Word

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545; min-height: 14.0px } p.p3 { margin: 0.0px 0.0px 2.0px 0.0px;

Leetcode 843. Guess the Word

Problem: This problem is an interactive problem new to the LeetCode platform. We are given a word list of unique words, each word is 6 letters long, and one word in this list is chosen as secret. You may call master.guess(word) to guess a word.  The

[Swift]LeetCode843. 猜猜这个单词 | Guess the Word

This problem is an interactive problem new to the LeetCode platform. We are given a word list of unique words, each word is 6 letters long, and one word in this list is chosen as secret. You may call master.guess(word) to guess a word.  The guessed w

GUI &amp; Event例子

Student No.: _______________ Name: ________________________________________1TK2934 Object-Oriented ProgrammingProject : GUI & EventIn this lab you will be using the following Java Swing & awt classes:• container – JFrame, JPanel• components – JBut

TPLink 备份文件bin文件解析[续]

Most routers allow to save and restore configuration from files. This is cool because you can edit the configuration file and upload to the router again enabling some "hidden" configuration options. For example on my D-Link DSL-2640B I managed t

asp.net批量发布博客到各大博客平台

新浪博客 http://upload.move.blog.sina.com.cn/blog_rebuild/blog/xmlrpc.php 网易博客 http://os.blog.163.com/api/xmlrpc/metaweblog/ 自建Wordpress站点 http://您的博客地址/xmlrpc.php 博客园cnblogs 博客园 - 开发者的网上家园<您的用户名>/services/metaweblog.aspx 自建zBlog站点 http://your.zblog.sit

MITx 6.00 Problem Set 3 hangman游戏

全部代码参见Github:https://github.com/pxjw/MITx-6.00.1-2-Problem-Set/tree/master/6.00.1x/proSet3 HANGMAN PART 1: IS THE WORD GUESSED? Please read the Hangman Introduction before starting this problem. The helper functions you will be creating in the next t

MetaWeblog API调用

http://rpc.cnblogs.com/metaweblog/webenh 在网上闲逛,突然对博客的接口感兴趣,经考察,多数博客都对metaWeblog Api 提供了支持,虽然windows live writer是好用,不过出于对这个接口的好奇,也想自己做个能发博客的小工具. 到处浏览资料,用了一个下午终于成功发布了一篇简陋版博客:).其实很简单,方法对了很快就连上了. MetaWeblog API中文说明 1.什么是MetaWeblog API? MetaWeblog API(MWA