csdn在线编程里面的一个排列组合题
2013-12-07 19:44
337 查看
是csdn在线编程里面的一个问题
回文字符串是指从左到右和从右到左相同的字符串,现给定一个仅由小写字母组成的字符串,
你可以把它的字母重新排列,以形成不同的回文字符串。
输入:非空仅由小写字母组成的字符串,长度不超过100;
输出:能组成的所有回文串的个数(因为结果可能非常大,输出对1000000007取余数的结果)。
例如:输入"aabb" 输出为2(因为“aabb”对应的所有回文字符串有2个:abba和baab)
函数头部 c: int palindrome(const char *s); c++ int palindrome(const string &s); java public static int palindrome(String s)
我写了代码出来,自认为应该是对的了,不知道为啥提交上去,测试用例没有通过,而那个在线编程最讨厌的是,不会告诉你具体哪个用例失败了,求高人指点一下,我下面的代码会在哪个用例上失败,感激不尽!
回文字符串是指从左到右和从右到左相同的字符串,现给定一个仅由小写字母组成的字符串,
你可以把它的字母重新排列,以形成不同的回文字符串。
输入:非空仅由小写字母组成的字符串,长度不超过100;
输出:能组成的所有回文串的个数(因为结果可能非常大,输出对1000000007取余数的结果)。
例如:输入"aabb" 输出为2(因为“aabb”对应的所有回文字符串有2个:abba和baab)
函数头部 c: int palindrome(const char *s); c++ int palindrome(const string &s); java public static int palindrome(String s)
我写了代码出来,自认为应该是对的了,不知道为啥提交上去,测试用例没有通过,而那个在线编程最讨厌的是,不会告诉你具体哪个用例失败了,求高人指点一下,我下面的代码会在哪个用例上失败,感激不尽!
int palindrome(const string &s) { const unsigned int zhishu = 1000000007; int len = s.length(); if(len > 100) return -1; int chararr[26]; for(int i=0; i<26; ++i){ chararr[i] = 0; } int temp; for(int i=0; i<len; ++i){ temp = s[i] - 'a'; if(temp < 0 || temp >= 26) return -1; ++chararr[temp]; } int sum = 0; int jishu = 0; for(int i=0; i<26; ++i){ if(chararr[i]%2 != 0){ ++jishu; } sum += chararr[i]/2; } if(jishu > 1) return -1; unsigned int result = 1; int chushu = 1; int j = 0; int i = sum; while(i > 1){ if(chushu < 2 && j < 26){ chushu = chararr[j]/2; ++j; } while(chushu > 1 && result % chushu == 0){ result /= chushu; --chushu; } result *= i; result %= zhishu; --i; } while(chushu > 1 || j < 26){ if(chushu < 2){ chushu = chararr[j]/2; ++j; } if(chushu < 2){ continue; } result /= chushu; --chushu; } return result; }
相关文章推荐
- python,django,mysql学习之环境安装配置
- c#中的扩展方法
- C++基础学习笔记----第十二课(操作符重载-上)
- 资源空间新添加《Visual C++ 游戏开发经典案例详解》PDF文档,单一PDF文件,带完整书签。
- Java Spring Tutorial -- Spring框架和commons-logging
- 《UNIX环境高级编程》笔记--kill函数,raise函数,alarm函数,pause函数
- 第一次java线程
- socket连接两台电脑通信java
- Django Admin外键选择的自动完成
- C++ 常见崩溃问题分析
- 关于Matlab学习的建议
- Python获取当前时间
- eclipse java.lang.OutOfMemoryError: Java heap space
- 详解Java异常处理——简介
- Python单例模式的4种实现方法
- c++内存管理 01
- python系列学习——移动开发
- Matlab与数据库(连接,增删改查)
- Coincidence(LCS最长公共子序列)
- Linear Cellular Automata