ssoj2435 回文串游戏(贪心)
来源:互联网 发布:常用的网络通信协议 编辑:程序博客网 时间:2024/05/20 03:46
题意:给一个串,和一个初始位置,每次操作可以左移一位或右移一位,当前值加一或减一,要求最少操作次数使串变成回文串。
思路:只要处理初始位置所在的半边。
注意:一开始写的程序,不必更改的和只有一位要更改的,情况一样,要处理一下。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int maxn=100055;int f[maxn],n,p,mid,ans=0;char s[maxn];inline int get(){ char c;while(!isdigit(c=getchar())); int v=c-48;while(isdigit(c=getchar()))v=v*10+c-48; return v;}int main(){ n=get();p=get(); memset(f,0,sizeof(f)); scanf("%s",s+1); mid=n/2; for(int i=1;i<=mid;++i)f[i]=f[n-i+1]=min(abs(26-abs(s[i]-s[n-i+1])),abs(s[i]-s[n-i+1])); if(p<=mid){ int l=1,r=mid; while(l<mid && f[l]==0)++l; while(r>l && f[r]==0)--r; if(l==r && f[l]==0){printf("0\n");return 0;} for(int i=l;i<=r;++i)ans+=f[i]; int len=min(abs(p-l),abs(r-p)); ans+=len+r-l; } else{ int l=mid+1,r=n; while(l<n && f[l]==0)++l; while(r>l && f[r]==0)--r; if(l==r && f[l]==0){printf("0\n");return 0;} for(int i=l;i<=r;++i)ans+=f[i]; int len=min(abs(p-l),abs(r-p)); ans+=len+r-l; } printf("%d\n",ans); return 0;}
0 0
- ssoj2435 回文串游戏(贪心)
- Evil Straw Warts Live (Uva10716 回文串+贪心)
- nyoj 1023 还是回文 (区间dp加贪心)
- HDU-5744 Keep On Movin(回文串 贪心)
- 网易编程题一,回文序列(贪心法)
- 【BZOJ 3790】神奇项链 回文树+贪心
- UVA 10716 Evil Straw Warts Live 回文数 贪心
- Evil Straw Warts Live uva+回文串+贪心
- codeforces 486C Palindrome Transformation 贪心求构造回文
- uva--10716Evil Straw Warts Live +回文串+贪心
- uva 10716 Evil Straw Warts Live(贪心回文串)
- codeforces 486C Palindrome Transformation 贪心求构造回文
- BZOJ 2565 双回文子串 Manacher 蜜汁贪心QQQ
- HDU 1431 素数回文(回文数打表)
- [BZOJ3676][Apio2014]回文串(回文自动机)
- HDU 3068 最长回文 (回文自动机)
- BZOJ3676 回文串 (回文树)
- 回文树(回文自动机)学习小记
- 第六周--数制转换
- POJ 2251 Dungeon Master(bfs)
- 正则表达式 “双向最小匹配”
- EntityFramework走马观花之 CRUD(上)
- Finding the factors of an integer
- ssoj2435 回文串游戏(贪心)
- Cygwin-安装和配置ssh服务
- 第一次面试有感
- 深入浅出MyBatis-Configuration
- UI 动画之反射变换的旋转
- 直击阻塞,非阻塞,异步,同步四者联系与区别
- xml解析 dom4j通过XPath找标签
- 写出最小公因数
- 最小生成树kruskal算法(并查集)