UVA 11888 - Abnormal 89's(Manachar)

UVA 11888 - Abnormal 89‘s

option=com_onlinejudge&Itemid=8&page=show_problem&category=524&problem=2988&mosmsg=Submission+received+with+ID+14075396" target="_blank" style="">题目链接

题意:给定一个字符串。推断类型。一共三种。两个回文拼接成的,一个回文,其他

思路:利用Manachar处理出每一个位置的最长回文,然后扫描一遍去推断就可以

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 200005;

int t, p[N * 2], n, len;
char str[N], s[N * 2];

void manachar() {
    len = 2;
    s[0] = '@'; s[1] = '#';
    for (int i = 0; i < n; i++) {
	s[len++] = str[i];
	s[len++] = '#';
    }
    s[len] = '\0';
    int mx = 0, id;
    for (int i = 1; i < len; i++) {
	if (mx > i) p[i] = min(p[2 * id - i], mx - i);
	else p[i] = 1;
	while (s[i + p[i]] == s[i - p[i]]) p[i]++;
	if (i + p[i] > mx) {
	    id = i;
	    mx = i + p[i];
	}
    }
}

int judge() {
    int need = 0;
    for (int i = 2; i < len - 1; i++) {
	if ((p[i] - 1) / 2 == need) {
	    int l = i + p[i] - 1;
	    int r = len - 1;
	    int mid = (l + r) / 2;
	    int lneed = need * 2;
	    if (s[i] != '#') lneed++;
	    int rneed = n - lneed;
	    if (rneed && rneed == p[mid] - 1) return 0;
	}
	if (s[i] != '#') need++;
    }
    if (p[len / 2] - 1 == n) return 1;
    return 2;
}

int main() {
    scanf("%d", &t);
    while (t--) {
	scanf("%s", str);
	n = strlen(str);
	manachar();
	if (judge() == 0) printf("alindrome\n");
	else if (judge() == 1) printf("palindrome\n");
	else printf("simple\n");
    }
    return 0;
}

UVA 11888 - Abnormal 89's(Manachar)

时间: 2024-08-21 21:20:17

UVA 11888 - Abnormal 89&#39;s(Manachar)的相关文章

UVA 11888 - Abnormal 89&#39;s(Manachar)

UVA 11888 - Abnormal 89's 题目链接 题意:给定一个字符串,判断类型,一共三种,两个回文拼接成的,一个回文,其它 思路:利用Manachar处理出每个位置的最长回文,然后扫描一遍去判断即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 200005; int t, p[N * 2], n, len;

UVa 11888 Abnormal 89&#39;s

方法:Manacher Manacher算法在O(length) 时间内求出各个回文子串的长度.O(length) 时间检查时那一种情况. code: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <string> 6 #include <vector> 7 #include <st

UVA 1397 - The Teacher&amp;#39;s Side of Math(高斯消元)

UVA 1397 - The Teacher's Side of Math 题目链接 题意:给定一个x=a1/m+b1/n.求原方程组 思路:因为m*n最多20,全部最高项仅仅有20.然后能够把每一个此项拆分.之后得到n种不同无理数,每一项为0.就能够设系数为变元.构造方程进行高斯消元 一開始用longlong爆了.换成分数写法也爆了,又不想改高精度.最后是机智的用了double型过的,只是用double精度问题,所以高斯消元的姿势要正确,而且最后输出要注意-0的情况 代码: #include

UVA - 10239 The Book-shelver&amp;#39;s Problem

Description Problem D The Book-shelver's Problem Input: standard input Output: standard output Time Limit: 5 seconds Memory Limit: 32 MB You are given a collection of books, which must be shelved in a library bookcase ordered (from top to bottom in t

uva 167 - The Sultan&amp;#39;s Successors(典型的八皇后问题)

这道题是典型的八皇后问题,刘汝佳书上有具体的解说. 代码的实现例如以下: #include <stdio.h> #include <string.h> #include <stdlib.h> int vis[100][100];//刚開始wrong的原因就是这里数组开小了,开了[8][8],以为可以.后来看到[cur-i+8]意识到可能数组开小了.改大之后AC. int a[8][8]; int C[10]; int max_,tot; void search_(int

结对编程2——单元测试

 周迪 201421123089    黄睿 201421123069  coding.net 地址:http://git.oschina.net/hr2324/SoftEngHW 1.我们的代码要使它的利用率大大提高,所以我们要通过单元测试进一步完善代码,发现细小的错误.于是这次就用JUnit进行了简单四则运算的单元测试. a.需求分析:(1)在上次的基础上将计算模块单独创建一个类: (2)针对计算类做单元测试: (3)主要做整数和真分数的加减乘除: (4) 增量式开发,用git命令提交到co

Android_SVG概述及生成使用SVG详解

1.效果图 2.SVG-Path路径 下面的命令可用于路径数据:M = movetoL = linetoH = horizontal linetoV = vertical linetoC = curvetoS = smooth curvetoQ = quadratic Belzier curveT = smooth quadratic Belzier curvetoA = elliptical ArcZ = closepath注释:以上所有命令均允许小写字母.大写表示绝对定位,小写表示相对定位.

Swift的函数与函数指针、闭包Closure等相关内容介绍

<span style="font-size:24px;">//函数 //demo1 无參数类型 func testConcat(){ println("測试函数"); } testConcat()//调用demo1 //demo2 多參数,一个返回值 /* 函数以func开头,表示一个函数 v1表示參数.String參数类型 ->返回值类型String */ func testConcats(v1:String,v2:String) ->S

linux 多核CPU性能调优

常常感觉系统资源不够用,一台机子上跑了不下3个比较重要的服务,但是每天我们还要在上面进行个备份压缩等处理,网络长时间传输,这在就很影响本就不够用的系统资源: 这个时候我们就可以把一些不太重要的比如copy/备份/同步等工作限定在一颗cpu上,或者是多核的cpu的一颗核心上进行处理,虽然这不一定是最有效的方法,但可以最大程度上利用了有效资源,降低那些不太重要的进程占用cpu资源: 查看系统下cpu信息: cat /proc/cpuinfo taskset就可以帮我们完成这项工作,而且操作非常简单: