zoukankan      html  css  js  c++  java
  • 回文字符串

     1 #include<stdio.h>
     2 
     3 #include<string.h>
     4 int a[27],vis[11],len,k,t,cnt;
     5 char str[11],temp[6],b_str[6],jd_str[121][6],c;
     6 int judge()
     7 {
     8     int i;
     9     for(i = 0;i < t;i ++)
    10         if(!strcmp(jd_str[i],temp))
    11             return 0;
    12     strcpy(jd_str[t++],temp);
    13     return 1;
    14 }
    15   
    16 void dfs(int n)
    17 {
    18     int i,j;
    19     if(n == len && judge())
    20     {
    21         printf("%s",temp);
    22         if(cnt == 1)
    23             printf("%c",c);
    24         for(j = len-1;j > 0;j --)
    25             printf("%c",temp[j]);
    26         printf("%c
    ",temp[0]);
    27         return ;
    28     }
    29     for(i = 0;i < len;i ++)
    30     {
    31         if(!vis[i])
    32         {
    33             vis[i] = 1;
    34             temp[k++] = b_str[i];
    35             dfs(n+1);
    36             k--;
    37             vis[i] = 0;
    38         }
    39     }
    40 }
    41   
    42 int main()
    43 {
    44     int i,j,m;
    45     while(~scanf("%s",str))
    46     {
    47         memset(temp,0,sizeof(temp));
    48         memset(vis,0,sizeof(vis));
    49         memset(a,0,sizeof(a));
    50         memset(b_str,0,sizeof(b_str));
    51         m = k = 0;
    52         t = cnt = 0;
    53         len = strlen(str);
    54         for(i = 0;i < len;i ++)
    55             a[str[i]-'a']++;
    56         for(i = 0;i < 27;i ++)
    57         {
    58             if(a[i] % 2)
    59             {
    60                 cnt++;
    61                 c = i+'a';
    62             }
    63             if(cnt > 1)
    64                 break ;
    65         }
    66         if(cnt > 1)          //判断是否有回文存在
    67             printf("no palindrome!
    ");
    68     else if(len == 1)
    69       printf("%s
    ",str);
    70         else
    71         {
    72             for(i = 0;i < 27;i ++)
    73             {
    74                 for(j = 0;j < a[i] >> 1;j ++)
    75                     b_str[m++] = i+'a';
    76             }
    77             len = strlen(b_str);
    78             dfs(0);
    79         }
    80     }
    81     return 0;
    82 }
  • 相关阅读:
    Java接口面面观
    Java之今天的异常处理了吗
    Java中自动装箱与拆箱详解
    java中this关键字解析
    Java语言实现机制
    Centos7 中 service iptables save 失效
    基于JQuery网页漂浮广告窗口Js详解
    JS日历
    js时间转换
    两种方法实现在HTML页面加载完毕后运行某个js
  • 原文地址:https://www.cnblogs.com/anhuizhiye/p/3366945.html
Copyright © 2011-2022 走看看