码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 353 - Pesky Palindromes

时间:2014-10-17 01:52:53      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:blog   io   os   ar   for   sp   数据   2014   on   

题目:统计一个串的回文子串的个数。

分析: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;
}

UVa 353 - Pesky Palindromes

标签:blog   io   os   ar   for   sp   数据   2014   on   

原文地址:http://blog.csdn.net/mobius_strip/article/details/40159397

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!