uva 353 - Pesky Palindromes

本文详细解读了如何通过简单的算法解决计算字符串中回文串数量的问题,包括代码实现、注意事项和复杂度分析。

又一道水题

1 //8ms 2010-05-14 15:09:52
2  //ac(1)
3 //Type: string Palindromes (回文串数)
4 //数据规模比较小,暴力解决
5 //注意判重
6 #include <stdio.h>
7 #include <string.h>
8 #define NL 100
9
10 char s[NL];
11 char palin[NL][NL*NL];
12 int np;
13
14 bool ispalin(int i, int j)
15 {
16 char s0[NL];
17 int K = 0, i0, j0;
18 i0 = i;
19 j0 = j;
20 while (i0<=j0) {
21 if (s[i0] != s[j0]) return false;
22 i0++;
23 j0--;
24 }
25 i0 = i;
26 j0 = j;
27 while (i0<=j0) {
28 s0[K++] = s[i0++];
29 }
30 s0[K] = '\0';
31 for (K=0; K<np; K++) {
32 if (!strcmp(s0, palin[K])) return false;
33 }
34 strcpy(palin[np], s0);
35 np++;
36 return true;
37 }
38
39 int main()
40 {
41 int len, i, j;
42 while (gets(s)) {
43 len = strlen(s);
44 if (len == 0) continue;
45 int cnt = 0;
46 np = 0;
47 for (i=0; i<len; i++) {
48 for (j=i; j<len; j++) {
49 if (ispalin(i, j)) {
50 cnt++;
51 }
52 }
53 }
54 printf("The string '%s' contains %d palindromes.\n", s, cnt);
55 }
56 return 0;
57 }

 

转载于:https://www.cnblogs.com/superbin/archive/2010/05/14/1735488.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值