uva 12718 2013Dhaka J
题意:求字串中可以随机排列形成回文串的个数。
思路:枚举起点,在枚举终点的同时更新标记数组记录每个字母分别出现几次在统计即可。
代码如下:
1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2014-07-05 17:00 5 * Filename : uva_12718.cpp 6 * Description : 7 * ************************************************/ 8 9 #include <iostream> 10 #include <cstdio> 11 #include <cstring> 12 #include <cstdlib> 13 #include <cmath> 14 #include <algorithm> 15 #include <queue> 16 #include <stack> 17 #include <vector> 18 #include <set> 19 #include <map> 20 #define MP(a, b) make_pair(a, b) 21 #define PB(a) push_back(a) 22 23 using namespace std; 24 typedef long long ll; 25 typedef pair<int, int> pii; 26 typedef pair<unsigned int,unsigned int> puu; 27 typedef pair<int, double> pid; 28 typedef pair<ll, int> pli; 29 typedef pair<int, ll> pil; 30 31 const int INF = 0x3f3f3f3f; 32 const double eps = 1E-6; 33 const int LEN = 1010; 34 string s; 35 36 int main() 37 { 38 // freopen("in.txt", "r", stdin); 39 40 int T, kase = 1; 41 cin >> T; 42 while(T--){ 43 cin >> s; 44 int ans = 0; 45 for(int i=0; i<s.size(); i++){ 46 int f[30] = {0}, cnt = 0; 47 for(int j=i; j<s.size(); j++){ 48 int pos = s[j]-'a'; 49 if(f[pos] % 2 == 1) cnt --; 50 else cnt ++; 51 f[pos] ++; 52 if(cnt < 2) ans ++; 53 } 54 } 55 cout << "Case " << kase++ << ": " << ans << endl; 56 } 57 return 0; 58 }
奔跑吧!少年!趁着你还年轻