神奇到爆炸的贪心,策略很简单。但是实现上好像比较恶心。换了一种思路。先保存所有点应该转移到的位置,BIT搞个逆序对就好了。

如何找到每个点应该转移到的位置?这个处理方式也是比较玄学。看代码吧。

//OJ 1508
//by Cydiater
//2016.10.31
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <ctime>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define up(i,j,n) 		for(int i=j;i<=n;i++)
#define down(i,j,n)		for(int i=j;i>=n;i--)
#define cmax(a,b) 		a=max(a,b)
#define cmin(a,b)		a=min(a,b)
#define FILE "string!"
const int MAXN=1e6+5;
const int oo=0x3f3f3f3f;
char s[MAXN];
int N,pos[MAXN],cnt[MAXN],c[MAXN],Head[MAXN],Tail[MAXN],len=0;
struct _data{
	int v,next,pre;
}q[MAXN];
ll ans=0;
namespace solution{
	inline int lowbit(int i){return i&(-i);}
	inline int get(int Pos){int tmp=0;for(int i=Pos;i<=N;i+=lowbit(i))tmp+=c[i];return tmp;}
	inline void insert(int Pos){for(int i=Pos;i>=1;i-=lowbit(i))c[i]++;}
	void init(){
		scanf("%s",s+1);
		N=strlen(s+1);
		up(i,1,N){
			if(Head[s[i]]==0){
				Head[s[i]]=Tail[s[i]]=++len;
				q[len].pre=q[len].next=0;
				q[len].v=i;
			}else{
				q[Tail[s[i]]].next=++len;
				q[len].next=0;q[len].pre=Tail[s[i]];q[len].v=i;
				Tail[s[i]]=len;
			}
			cnt[s[i]]++;
		}
		bool flag=0;//pre judge
		up(i,'A','Z')if(cnt[i]%2==1&&(flag||N%2==0)){
			puts("-1");
			exit(0);
		}else if(cnt[i]%2==1)flag=1;
	}
	inline int get_siz(int i){
		if(Head[s[i]]==0)			return 0;
		if(Head[s[i]]!=Tail[s[i]])	return 2;
		else 						return 1;
	}
	void slove(){
		int j=1;
		up(i,1,N){
			int siz=get_siz(i);
			if(siz==0)continue;
			else if(siz==1){
				pos[q[Tail[s[i]]].v]=N/2+1;
				Head[s[i]]=Tail[s[i]]=0;
			}
			else{
				pos[q[Tail[s[i]]].v]=N-j+1;
				pos[q[Head[s[i]]].v]=j;
				if(q[Head[s[i]]].next==Tail[s[i]])Head[s[i]]=Tail[s[i]]=0;
				else{
					Head[s[i]]=q[Head[s[i]]].next;
					Tail[s[i]]=q[Tail[s[i]]].pre;
					q[Head[s[i]]].pre=0;q[Tail[s[i]]].next=0;
				}
				j++;
			}
		}
		up(i,1,N){
			ans+=get(pos[i]);
			insert(pos[i]);
		}
		cout<<ans<<endl;
	}
}
int main(){
	freopen(FILE".in","r",stdin);
	freopen(FILE".out","w",stdout);
	//freopen("input.in","r",stdin);
	//freopen("out.out","w",stdout);
	using namespace solution;
	init();
	slove();
	return 0;
}

[HAOI2009]求回文串的更多相关文章

  1. 2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对)

    2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对) https://www.luogu.com.cn/problem/P5041 题意: 给一个字符串 \(S\) ,每 ...

  2. Palindrome - URAL - 1297(求回文串)

    题目大意:RT   分析:后缀数组求回文串,不得不说确实比较麻烦,尤其是再用线段数进行查询,需要注意的细节地方比较多,比赛实用性不高......不过练练手还是可以的.   线段数+后缀数组代码如下: ...

  3. 马拉车,O(n)求回文串

    马拉车,O(n)求回文串 对整个马拉车算法步骤做个总结: 第一步:将每个原字母用两个特殊字符包围如: aaa --> #a#a#a# abab -->#a#b#a#b 同时可以由这个翻倍的 ...

  4. manacher算法,求回文串

    用来求字符串最长回文串或者回文串的总数量 #include<map> #include<queue> #include<stack> #include<cma ...

  5. hdu 3294 manacher 求回文串

    感谢: http://blog.csdn.net/ggggiqnypgjg/article/details/6645824/ O(n)求给定字符串的以每个位置为中心的回文串长度. 中心思想:每次计算位 ...

  6. hihocoder 1032 manachar 求回文串O(n)

    #include <cstdio> #include <iostream> #include <algorithm> #include <queue> ...

  7. HDU 5371(2015多校7)-Hotaru&#39;s problem(Manacher算法求回文串)

    题目地址:HDU 5371 题意:给你一个具有n个元素的整数序列,问你是否存在这样一个子序列.该子序列分为三部分,第一部分与第三部分同样,第一部分与第二部分对称.假设存在求最长的符合这样的条件的序列. ...

  8. LOJ 2452 对称 Antisymmetry——用hash求回文串数

    概念 用hash求最长回文串/回文串数 首先,易知,回文串具有单调性. 如果字符串 $s[l...r]$ 为回文串串,那么 $s[x...y]$($l < x, y < r$ 且 $|l- ...

  9. 拓展KMP求回文串

    题目:hdu3613: 题意:有26字母对应的价值,然后给出以个串,把它分成两段字串,如果字串是回文串,串的价值就是每个字符和,不是就为0.求最大价值. 博客 分析:拓展KMP的应用求回文字串. #i ...

随机推荐

  1. Linux LVM学习总结——删除物理卷

    本篇介绍LVM管理中的命令vgreduce, pvremove.其实前面几篇中以及有所涉及. vgreduce:通过删除LVM卷组中的物理卷来减少卷组容量.注意:不能删除LVM卷组中剩余的最后一个物理 ...

  2. 【转】JVM运行原理及JVM中的Stack和Heap的实现过程

    来自: http://blog.csdn.net//u011067360/article/details/46047521 Java语言写的源程序通过Java编译器,编译成与平台无关的‘字节码程序’( ...

  3. cnless.sh:改进版less,可自动识别GBK编码或UTF-8编码。

    #!/bin/bash #功能:让GBK编码的文件可以使用less正常显示中文(自动识别GBK和UTF-8编码) #v0. 在LINUX下,使用UTF-8编码,less UTF-8的文件时显示中文正常 ...

  4. 004.测试解析php,安装discuz

    一.配置解析php 编辑nginx配置文件/usr/local/nginx/conf/nginx.conf [root@huh ~]# vim /usr/local/nginx/conf/nginx. ...

  5. 烂泥:openvpn配置文件详解

    本文由秀依林枫提供友情赞助,首发于烂泥行天下 在上一篇文章<烂泥:ubuntu 14.04搭建OpenVPN服务器>中,我们主要讲解了openvpn的搭建与使用,这篇文章我们来详细介绍下有 ...

  6. python 利用 setup.py 手动安装django_chartit

    手动安装django_chartit库 1 下载压缩包 2 解压到python安装目录下,文件夹名为django_chartit,并检查文件夹下是否有setup.py文件 3 在cmd中进入djang ...

  7. [WPF系列]-基础系列 Property Trigger, DataTrigger & EventTrigger

    So far, we worked with styles by setting a static value for a specific property. However, using trig ...

  8. Caffe源码解析2:SycedMem

    转载请注明出处,楼燚(yì)航的blog,http://www.cnblogs.com/louyihang loves baiyan/ 看到SyncedMem就知道,这是在做内存同步的操作.这类个类的 ...

  9. UVA - 11987 Almost Union-Find[并查集 删除]

    UVA - 11987 Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, y ...

  10. 掌握SortSet接口和Set接口的关系,以及常用方法。

    TreeSet类是可以排序的类.TreeSet实际上也是SortSet接口的子类. 此接口的所有类都是可以排序的. 所有的方法: 实例: package 类集; import java.util.So ...