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,如需转载请自行联系原作者


相关文章
|
算法 索引
LeetCode 214. Shortest Palindrome
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。
59 0
LeetCode 214. Shortest Palindrome
AtCoder Beginner Contest 214 F - Substrings(subsequence DP)
AtCoder Beginner Contest 214 F - Substrings(subsequence DP)
76 0
Uva - 12050 Palindrome Numbers【数论】
题目链接:uva 12050 - Palindrome Numbers   题意:求第n个回文串 思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然后就得到是长度为多少的第几个的回文串了,有个细节注意的是, n计算完后要-1! 下面给出A...
817 0
[LeetCode] Shortest Palindrome
The idea is to find the longest palindromic substring of s that begins with s[0]. Then take the remaining susbtring, reverse it and append it to the beginning of s.
801 0

热门文章

最新文章