UVa 353 - Pesky Palindromes

简介:

称号:字符串统计回文子的数量。

分析:dp,暴力。因为数据是小,直接暴力可以解决。

说明:(UVa最终评出800该)。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>

using namespace std;

char str[82];
char ans[3200][82];

int main()
{
	while (~scanf("%s",str)) {
		int count = 0,len = strlen(str);
		for (int i = 1 ; i <= len ; ++ i)
		for (int s = 0 ; s < len ; ++ s) {
			int flag = 1;
			for (int t = s+i-1 ; t >= s ; -- t)
				if (str[s+s+i-1-t] != str[t]) {
					flag = 0;
					break;
				}
			if (flag) {
				for (int j = 0 ; j < i; ++ j)
					ans[count][j] = str[s+j];
				ans[count][i] = 0;
				int same = 0;
				for (int j = 0 ; j < count ; ++ j) 
				if (!strcmp(ans[j], ans[count])) {
					same = 1;
					break;
				}
				if (!same) count ++;
			}
		}
		
		printf("The string \'%s\' contains %d palindromes.\n",str,count);
		/*
		printf("The %d unique palindromes in \'boy\' are",count);
		for (int i = 0 ; i < count-1 ; ++ i) {
			printf(" \'%s\'",ans[i]);
			if (i < count-2)
				printf(",");
			else printf(" and ");
		}
		printf("\'%s\'.\n\n",ans[count-1]);
		*/
	}
	return 0;
}

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








本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4726602.html,如需转载请自行联系原作者


相关文章
UVa389 - Basically Speaking
UVa389 - Basically Speaking
40 0
uva673 Parentheses Balance
uva673 Parentheses Balance
51 0
uva127 "Accordian" Patience
uva127 "Accordian" Patience
47 0
Say No to Palindromes
题意: 给出一个字符串,长度为n,而且都是又a,b,c三个字符构成的,然后有m个询问 每个询问给出l r,问要想这个区间内不存在回文子串,至少要改多少个字符 结论: 一共会有六种情况
138 0
|
Go
HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)
HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)
88 0
回文树(回文自动机) - URAL 1960 Palindromes and Super Abilities
  Palindromes and Super Abilities Problem's Link:  http://acm.timus.ru/problem.aspx?space=1&num=1960   Mean:  给你一个长度为n的字符串S,输出S的各个前缀中回文串的数量。
963 0