UVALive 6659 Dromicpalin Substrings (递推 + 暴力枚举)
2016-09-28 00:02
519 查看
题意:
给你一个长度不超过1000的字符串,对于任意一个子字符串,你可以任意排列,问有多少个子字符串能排成回文串?
思路:
先递推预处理出 i到j区间内 有多少个 奇数个的字母,然后暴力枚举 子字符串,判断这个区间奇数个数,如果大于1 不合适 否则ans++
输出ans即可!
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[1007];
int vis[30];
int g[1007][1007];
int main(){
int T,kase = 0;
scanf("%d",&T);
while(T--){
scanf("%s",s+1);
int len = strlen(s+1);
memset(g,0,sizeof g);
for (int i = 1; i <= len; ++i){
memset(vis,0,sizeof vis);
for (int j = i; j >= 1; --j){
int id = s[j] - 'a';
vis[id]++;
if (i == j)g[j][i] = 0;
else g[j][i] = g[j+1][i];
if (vis[id] & 1)g[j][i]++;
else g[j][i]--;
}
}
int ans = 0;
for (int i = 1; i <= len; ++i){
for (int j = i; j <= len; ++j){
if (g[i][j] > 1)continue;
++ans;
}
}
printf("Case %d: %d\n",++kase,ans);
}
return 0;
}
给你一个长度不超过1000的字符串,对于任意一个子字符串,你可以任意排列,问有多少个子字符串能排成回文串?
思路:
先递推预处理出 i到j区间内 有多少个 奇数个的字母,然后暴力枚举 子字符串,判断这个区间奇数个数,如果大于1 不合适 否则ans++
输出ans即可!
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[1007];
int vis[30];
int g[1007][1007];
int main(){
int T,kase = 0;
scanf("%d",&T);
while(T--){
scanf("%s",s+1);
int len = strlen(s+1);
memset(g,0,sizeof g);
for (int i = 1; i <= len; ++i){
memset(vis,0,sizeof vis);
for (int j = i; j >= 1; --j){
int id = s[j] - 'a';
vis[id]++;
if (i == j)g[j][i] = 0;
else g[j][i] = g[j+1][i];
if (vis[id] & 1)g[j][i]++;
else g[j][i]--;
}
}
int ans = 0;
for (int i = 1; i <= len; ++i){
for (int j = i; j <= len; ++j){
if (g[i][j] > 1)continue;
++ans;
}
}
printf("Case %d: %d\n",++kase,ans);
}
return 0;
}
相关文章推荐
- UVALive 6659 Dromicpalin Substrings 枚举判断
- UVALive 6659 Dromicpalin Substrings
- UVa 12718 Dromicpalin Substrings (暴力)
- UVA 12718 Dromicpalin Substrings
- UVA 12718 Dromicpalin Substrings(寻找字符串连续子串的回文)
- UVALive 6912 Prime Switch 暴力枚举+贪心
- UVALive 6163(暴力枚举)
- UVA 12718 Dromicpalin Substrings
- UVALive 7457 Discrete Logarithm Problem (暴力枚举)
- POJ1964/UVALive 3029 -City Game -求最大子矩形-(暴力枚举/dp)
- UVALive - 7457 Discrete Logarithm Problem 费马小定理+暴力枚举+快速幂
- UVALive Problem 7079 How Many Maos Does the Guanxi Worth(暴力枚举+最短路)——2014ACM/ICPC亚洲区广州站
- UVA - 10125 - Sumsets (暴力枚举 + STL)
- uvalive 6528(DAG,递推,想法/bitset, 好题)
- UVALive - 4868 暴力
- UVA725 Division【暴力枚举】
- uvalive4836(枚举)
- UVALive - 3401 Colored Cubes 枚举
- UVALive 6862 Triples (找规律 暴力)
- UVA 1354 - Mobile Computing(暴力枚举子集)