Palindrome<回文>字符串问题

本文介绍了一个用于判断字符串是否可以通过重新排列形成回文串的算法,并提供了详细的C语言实现代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

输出了其中一个Palindrome字符串:回文

  1. /************************************************************************/  
  2. /* author: wcdj 
  3.  * date:   2009-10-3 (mid-autumn festival) 
  4.  * description: 
  5.    如果给定的字符串从左到右和从右到左的序列完全一致,那么这样的字符串被称为palindrome。 
  6.    例如,下面的字符串都是palindromes。 
  7.    "kayak"    
  8.    "codilitytilidoc"     
  9.    "neveroddoreven" 
  10.    如果字符串A和字符串B含有相同的字母,但是顺序可能不一样,那么A被称为是B的anagram<易位构词,变位词>。 
  11.    例如,下面的字符串互为anagrams: 
  12.    A="mary" B="army" A="rocketboys" B="octobersky" A="codility" B="codility" 
  13.    实现一个函数  
  14.    int isAnagramOfPalindrome(char* str);  
  15.    如果str的某种anagram是palindrome,则返回1,否则返回0。 
  16. */  
  17. /************************************************************************/  
  18. #include <stdio.h>  
  19. #include <string>  
  20. #include <conio.h>//getch   
  21. void instruction()  
  22. {  
  23.     printf("----------------------------------/n");  
  24.     printf("/tPalindrome problem/n");  
  25.     printf("/tauthor: wcdj/n");  
  26.     printf("/tdate: 2009-10-3 (mid-autumn festival)/n");  
  27.     printf("----------------------------------/n/n");  
  28. }  
  29. int isAnagramOfPalindrome(char* str)  
  30. {  
  31.     int nRes[26]={0};  
  32.     int n=0;  
  33.     for (int index=0;index!=strlen(str);index++)  
  34.     {  
  35.         n=str[index]-'a';  
  36.         nRes[n]++;//标记找到某一字母  
  37.     }  
  38.     for (int nOdd=0,nSum=0,index2=0;index2!=26;index2++)//遍历存储结果的数组  
  39.     {  
  40.         if (nRes[index2]==0)  
  41.         {  
  42.             continue;  
  43.         }  
  44.         else  
  45.         {  
  46.             nSum+=nRes[index2];//统计有多少个字母  
  47.         }  
  48.         if (nRes[index2]%2==1)//如果符合条件,则只能存在一个奇数  
  49.         {  
  50.             nOdd++;  
  51.         }  
  52.         if (nOdd>1)  
  53.         {  
  54.             return 0;  
  55.         }  
  56.     }  
  57.     //show a palindrome  
  58.     char* pPal=new char[nSum+1];  
  59.     pPal[nSum]='/0';  
  60.     for (int index3=0,i=0;index3!=26;index3++)  
  61.     {  
  62.         if (nRes[index3]==0)  
  63.         {  
  64.             continue;  
  65.         }  
  66.           
  67.         int n=0;  
  68.         if (nRes[index3]%2!=1)  
  69.         {  
  70.             n=nRes[index3];  
  71.             while (n)  
  72.             {  
  73.                 pPal[i]=pPal[nSum-1-i]='a'+index3;  
  74.                 i++;  
  75.                 n-=2;  
  76.             }  
  77.         }  
  78.         else  
  79.         {  
  80.             if (nSum%2==1)  
  81.             {  
  82.                 pPal[nSum/2]='a'+index3;  
  83.             }  
  84.         }  
  85.     }  
  86.     printf("one of palindromes is: %s/n",pPal);  
  87.     delete []pPal;  
  88.     return 1;  
  89. }  
  90.    
  91. void main()      
  92. {  
  93.     instruction();  
  94.       
  95.     printf("input a string:/n");  
  96.     char str[128]={0};  
  97.     scanf("%s",str);  
  98.     printf("str=%s/n",str);  
  99.     int nRes=isAnagramOfPalindrome(str);  
  100.     if (nRes==1)  
  101.     {  
  102.         printf("the string is a palindrome/n");  
  103.     }  
  104.     else  
  105.     {  
  106.         printf("the string is not a palindrome/n");  
  107.     }  
  108.     getch();     
  109.     return;      
  110. }     

学习:http://blog.youkuaiyun.com/delphiwcdj/article/details/4630969


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值