题目大意:

As a token of his gratitude, Takahashi has decided to give Snuke a level-KK palindrome. A level-LL palindrome, where LL is a non-negative integer, is defined as follows:

  • Let rev(s)rev(s) denote the reversal of a string ss.
  • A string ss is said to be a palindrome when s=rev(s)s=rev(s).
  • The empty string and a string that is not a palindrome are level-00 palindromes.
  • For any non-empty level-(L−1)(L−1) palindrome tt, the concatenation of t,rev(t)t,rev(t) in this order is a level-LL palindrome.
  • For any level-(L−1)(L−1) palindrome tt and any character cc, the concatenation of t,c,rev(t)t,c,rev(t) in this order is a level-LL palindrome.

Now, Takahashi has a string SS. Determine whether it is possible to make SS an exactly level-KK palindrome by doing the following action zero or more times: choose a character in SS and change it to another lowercase English letter. If it is possible, find the minimum number of changes needed to make SS a level-KK palindrome.

(自己翻译去)

题目思路:

粗略分析:

通过“最少改变”这个关键词,我们不难想到贪心算法。

对于本道题,我们的贪心策略是:
找到字母相同的位置,用桶统计这个集合中每个字母出现的次数。

找到出现最多和次多的字母及最多出现的个数,

并将集合中的字母全部变成出现最多的那个字母,

同时统计答案。

问题细化:

Q1:如何找到必须相同的字母的位置?

A:使用分治算法,并用并查集来维护这个集合(并查集在替换操作上还有用处)

Q2:impossible情况

①k太大(大概是k>=20)

②len=pow(2,k)

③len<pow(2,k-1)

Q3:特殊判断!

1)替换后的字符串有更深层的回文。

这个时候记录下的次多的字母个数就有用处了。只需要掉一个非后回文串中心的字母就可以了。(要比较找最优方法)

并查集的性质也可以保证每个集合的总祖先是连续的处于字符串的头部的。非常方便进行替换操作。

2)特判:长度唯一的字符串回文深度为1

code:

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int k;
string s;
int ans=0;
int b[N][30];//桶
int maxn[N];//最大
int emaxn[N];//次大
int bel[N];//最小的子字符串中的字母
int f[N];//并查集
vector<int> res;//存最小子字符串的下标 int find(int x)//找祖先
{
if(f[x]==x) return x;
return f[x]=find(f[x]);
} void h(int x,int y)//认祖先
{
f[find(x)]=find(y);
return ;
} void dfs(int l,int r,int lev){//二分
if(lev==k) return ;
int len=(r-l+1);
if(len%2==1){
dfs(l,l+len/2-1,lev+1);dfs(l+len/2+1,r,lev+1);
}
if(len%2==0){
dfs(l,l+len/2-1,lev+1);dfs(l+len/2,r,lev+1);
}int mid=(l+r)/2;//奇回文串和偶回文串需要分开讨论
for(int i=0;r-i>=mid;i++) h(r-i,l+i);//建立集合
return ;
} bool judge(int len){//判断是否为回文串
for(int i=1;i<=len;i++) if(bel[i]!=bel[len-i+1]){return true;}
return false;
} int main(){
cin>>k>>s;
int len=s.size();
s=" "+s;
if(k>=20||len/(1<<k)==1||len<(1<<(k-1))){puts("impossible");return 0;}//无解的判断 for(int i=1;i<=len;i++) f[i]=i;//并查集初始化 int dip=k;
int anslen=len;//anslen表示最小子串的长度 while(dip--){
anslen/=2;//计算最小子串的长度
} dfs(1,len,0);//二分
for(int i=1;i<=len;i++){
b[find(i)][s[i]-'a']++;
if(find(i)==i) res.push_back(i);//res存的是一个完整的最小子串
} int sum=res.size(); for(int i=0;i<sum;i++){
int x=res[i];
for(int j=0;j<26;j++){
if(b[x][j]>maxn[x]) {emaxn[x]=maxn[x];maxn[x]=b[x][j];bel[x]=j;}
else emaxn[x]=max(emaxn[x],b[x][j]);//找最大和找次大
}
} for(int i=0;i<sum;i++) ans+=maxn[res[i]];//统计ans if(!anslen){cout<<len-ans;return 0;}//最小子串长度为1的特判
if(!judge(anslen)){//包含更深的回文
int minn=12345678;
for(int i=1;i<=anslen;i++){
if(anslen%2==1&&i==anslen/2+1) continue;
minn=min(minn,maxn[i]-emaxn[i]);//寻找最佳替换方案
}
ans-=minn;
}
cout<<len-ans;
return 0;
}

E - Level K Palindrome的更多相关文章

  1. 【LeetCode】1400. 构造 K 个回文字符串 Construct K Palindrome Strings

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计奇数字符出现次数 日期 题目地址:https:// ...

  2. uva 122 trees on the level——yhx

    题目如下:Given a sequence of binary trees, you are to write a program that prints a level-order traversa ...

  3. 链表之求链表倒数第k个节点

    题目描述:输入一个单向链表,输出该链表中倒数第k个节点,链表的倒数第0个节点为链表的尾指针. 最普遍的方法是,先统计单链表中结点的个数,然后再找到第(n-k)个结点.注意链表为空,k为0,k为1,k大 ...

  4. E - Trees on the level

     Trees on the level  Background Trees are fundamental in many branches of computer science. Current ...

  5. 1101-Trees on the Level

    描述 Trees are fundamental in many branches of computer science. Current state-of-the art parallel com ...

  6. 设计一个算法,求非空二叉树中指定的第k层(k&gt;1)的叶子节点的个数

    思想:採用基于层序遍历的方法. 用level扫描各层节点,若某一层的节点出队后.rear指向该层中最右节点.则将rear赋值给last(对于第一层.last=1).在出队时,若front=last,表 ...

  7. UVA122-Trees on the level(链二叉树)

    Trees on the level Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Sub ...

  8. Trees on the level(指针法和非指针法构造二叉树)

    Trees on the level Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. hdu 1622 Trees on the level(二叉树的层次遍历)

    题目链接:https://vjudge.net/contest/209862#problem/B 题目大意: Trees on the level Time Limit: 2000/1000 MS ( ...

随机推荐

  1. NodeJs 入门到放弃 — 常用模块及网络爬虫(二)

    码文不易啊,转载请带上本文链接呀,感谢感谢 https://www.cnblogs.com/echoyya/p/14473101.html 目录 码文不易啊,转载请带上本文链接呀,感谢感谢 https ...

  2. 【Azure 服务总线】Azure Service Bus中私信(DLQ - Dead Letter Queue)如何快速清理

    在博文ServiceBus 队列中死信(DLQ - Dead Letter Queue)问题一文中,介绍了服务总线产生私信的原因及可以通过代码的方式来清楚私信队列中的消息,避免长期占用空间(因为私信中 ...

  3. java 方法详解

    什么是方法 方法的定义和调用 值传递与引用传递 值传递:指的是在方法调用时,传递的是参数是按值的拷贝传递. 特点:传递的是值的拷贝,也就是传递后就互不相关了. 引用传递:指的是在方法调用时,传递的参数 ...

  4. Azure Front Door(一)为基于.net core 开发的Azure App Service 提供流量转发

    一,引言 之前我们讲解到使用 Azure Traffic Manager.Azure LoadBalancer.Azure Application Gateway,作为项目的负载均衡器来分发流量,转发 ...

  5. Elasticsearch 为了搜索

    前言 Elasticsearch 是一个开源的搜索引擎,建立在一个全文搜索引擎库 Apache Lucene 基础之上. Lucene 可以说是当下最先进.高性能.全功能的搜索引擎库--无论是开源还是 ...

  6. Fcitx5 上线 FreeBSD

    Fcitx5 上线 FreeBSD textproc/fcitx5textproc/fcitx5-qttextproc/fcitx5-gtktextproc/fcitx5-configtoolchin ...

  7. JS获取时间日期常用方法

    1 当前时间: new Date() 2 当前周: function getCurrentWeek() { var date = new Date() var beginDate = new Date ...

  8. 提升Idea启动速度与Tomcat日志乱码问题

    提升Idea启动速度与Tomcat日志乱码问题 前言 由于重装了一次Idea,所以有些设置时间就忘了,在此做个记录,以便以后忘记后可以来翻阅 Idea启动速度 一.将Idea所在的 安装文件夹 在wi ...

  9. LeetCode 175. Combine Two Tables 【MySQL中连接查询on和where的区别】

    一.题目 175. Combine Two Tables 二.分析 连接查询的时候要考虑where和on的区别 where : 查询时,连接的时候是必须严格满足条件的,满足了才会加入到临时表中. on ...

  10. jqgrid 实现表格随select框内容改变而刷新

    要实现的功能如下:当选择框选择数据源由原始数据切换到组合后数据时,界面左侧jqgrid表格随之改变.效果如下: 实现代码: 界面顶部select选择框:要点是用localStory将选择框的选择信息记 ...