UVA 257 - Palinwords(弦HASH)

UVA 257 - Palinwords

题目链接

题意:输出一个文本里面的palinword,palinword的定义为。包括两个不同的回文子串,而且要求回文子串不能互相包括

思路:对于每一个单词推断一次。因为不能互相包括。对于每一个位置。事实上就仅仅要找长度3和4的情况就可以,这样复杂度为O(n),至于推断反复的。就用hash就可以

代码:

#include <cstdio>
#include <cstring>


char str[260];
int hash[555555], save[260], sn;

bool check() {
    sn = 0;
    int n = strlen(str);
    for (int i = 1; i < n - 1; i++) {
	if (str[i - 1] == str[i + 1]) {
	    int num = (str[i - 1] - 'A' + 1) * 27 * 27 + (str[i] - 'A' + 1) * 27 + str[i + 1] - 'A' + 1;
	    if (!hash[num]) {
		hash[num] = 1;
		save[sn++] = num;
	    }
	    continue;
	}
	if (str[i] == str[i + 1] && str[i - 1] == str[i + 2]) {
	    int num = (str[i - 1] - 'A' + 1) * 27 * 27 * 27 + (str[i] - 'A' + 1) * 27 * 27 + (str[i + 1] - 'A' + 1) * 27 + str[i + 2] - 'A' + 1;
	    if (!hash[num]) {
		hash[num] = 1;
		save[sn++] = num;
	    }
	}
    }
    for (int i = 0; i < sn; i++)
	hash[save[i]] = 0;
    return sn >= 2;
}

int main() {
    while (~scanf("%s", str))
	if (check())
	    printf("%s\n", str);
    return 0;
}


版权声明:本文博主原创文章,博客,未经同意不得转载。

转载于:https://www.cnblogs.com/zfyouxi/p/4840626.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值