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 }

 

你可能感兴趣的:(ROM)