Misha and Palindrome Degree CodeForces - 501E (回文串计数)
大意: 给定字符串, 求多少个区间重排后能使原串为回文串.
先特判掉特殊情况, 对于两侧已经相等的位置之间可以任意组合, 并且区间两端点至少有一个在两侧相等的位置处, 对左右两种情况分别求出即可.
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head #ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif int n, m, k, t;
int f[N], a[N], c[N]; int main() {
a[0] = -1;
scanf("%d", &n);
REP(i,1,n) scanf("%d",a+i),++f[a[i]];
REP(i,1,n) m += f[i]&1;
if (m>1) return puts("0"),0;
int pos = 1, len = 0;
while (a[pos]==a[n-pos+1]) f[a[pos++]]-=2;
if (pos>n) return printf("%lld\n",(ll)n*(n+1)/2),0;
PER(i,1,n-pos+1) {
if (2*++c[a[i]]>f[a[i]]) {
if (i>n-i+1||a[i]!=a[n-i+1]) break;
if (i==n-i+1&&f[a[i]]%2==0) break;
}
++len;
}
memset(c,0,sizeof c);
REP(i,pos,n) {
if (2*++c[a[i]]>f[a[i]]) {
if (i<n-i+1||a[i]!=a[n-i+1]) break;
if (i==n-i+1&&f[a[i]]%2==0) break;
}
++len;
}
printf("%lld\n", (ll)pos*(pos+len));
}
Misha and Palindrome Degree CodeForces - 501E (回文串计数)的更多相关文章
- 【NOIP模拟赛】【乱搞AC】【奇技淫巧】【乘法原理】回文串计数
回文串计数 (calc.pas/calc.c/calc.cpp) [题目描述] 虽然是一名理科生,Mcx常常声称自己是一名真正的文科生.不知为何,他对于背诵总有一种莫名的热爱,这也促使他走向了以记忆量 ...
- Palindrome Partition CodeForces - 932G 回文树+DP+(回文后缀的等差性质)
题意: 给出一个长度为偶数的字符串S,要求把S分成k部分,其中k为任意偶数,设为a[1..k],且满足对于任意的i,有a[i]=a[k-i+1].问划分的方案数. n<=1000000 题解: ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Find the Closest Palindrome 寻找最近的回文串
Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'clo ...
- 132 Palindrome Partitioning II 分割回文串 II
给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串.返回 s 符合要求的的最少分割次数.例如,给出 s = "aab",返回 1 因为进行一次分割可以将字符串 s 分 ...
- P1872 回文串计数(回文树)
题目描述 小a虽然是一名理科生,但他常常称自己是一名真正的文科生.不知为何,他对于背诵总有一种莫名其妙的热爱,这也促使他走向了以记忆量大而闻名的生物竞赛.然而,他很快发现这并不能满足他热爱背诵的心,但 ...
- Palindrome(最长回文串manacher算法)O(n)
Palindrome Time Limit:15000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- Codeforces Round #410 (Div. 2) A. Mike and palindrome【判断能否只修改一个字符使其变成回文串】
A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Misha and Palindrome Degree
Misha and Palindrome Degree 题目链接:http://codeforces.com/problemset/problem/501/E 贪心 如果区间[L,R]满足条件,那么区 ...
随机推荐
- P2147 [SDOI2008]洞穴勘测
P2147 [SDOI2008]洞穴勘测 思路 没办法,我就是喜欢板子都想发的人 都是基础操作,不多说了 代码 #include <bits/stdc++.h> #define ls ch ...
- 【命令】MongoDB常用命令记录
如果你想创建一个“myTest”的数据库,先运行use myTest命令,之后就做一些操作(如:db.createCollection('user')),这样就可以创建一个名叫“myTest”的数据库 ...
- Cannot retrieve metalink for repository: epel 错误解决办法
centos下安装完EPEL源, 然后更新一下yum缓存, 如果发现这样的错误:Error: Cannot retrieve metalink for repository: epel. Please ...
- P4450 双亲数
思路 同zap-queries 莫比乌斯反演的板子 数据范围小到不用整除分块... 代码 #include <cstdio> #include <algorithm> #inc ...
- NOIP队内凉心互测总结(8.22update)
8.22(结束后一天) __stdcall讲题qwq 全是CF原题 D1T1 一看像是结论题,打了下表,水过 没错就是结论题,直接暴力就好 D1T2 看起来不好做,没有AC思路 打了暴力 40分 T2 ...
- ifconfig 输出里没有IP地址
转载: http://blog.csdn.net/johnstrive/article/details/5625121 inet addr:....Bcast:.....Mask:255.255.25 ...
- mybatis结合mysql批量操作及查询sql
MySQL数据库 批量操作主要使用的是Mybatis的foreach,遍历参数列表执行相应的操作,所以批量插入/更新/删除的写法是类似的,只是SQL略有区别而已.MySql批量操作需要数据库连接配置a ...
- Latex: "Missing $ inserted" 解决方法
参考: Latex报"Missing $ inserted"的解决方法 Latex: "Missing $ inserted" 解决方法 原因一:在文中出现&q ...
- CPU指令集设计RISC和CISC
CPU指令集 硬件实现具有速度快,成本高,灵活性差,软件实现与之相反.因此出现频率高的基本功能首选硬件实现.指令集的不同反映了设计原理.制造技术和系统类别. RISC 精简指令集计算机(Reduced ...
- Lintcode214-Max of Array-Naive
Given an array with couple of float numbers. Return the max value of them. Example Example 1: Input: ...