SRM589 DV1 250 回文字符串
其实这道题挺简单的,不过刚开始我钻了一个错误的死胡同。想明白之后才发现
。
题目要求用最少的时间来将一个字符串变成回文字符串。(具体题目参看topcoder srm589 DV1 250分值的题目,不便公开以免影响后面做题的人)
- 首先对于(i,j),如果i=n-1-j, 那它们所代表的位置的字符最终必须相等,否则就不是回文了。S[i]=S[j];
- 如果源字符串S中,S[i]=S[j], 那么在最终的回文字符串中,它们必须还是相等。因为如果S[i]要变成其他字符,那么S[j]也必须跟着变。即它们要不都变,要不都不变。
- 可传递性:(i,j,k) 如果S[i]=S[j] 且 S[k]=S[j] 那么最后一定有S[i]=S[k]。因为它们都是必须保证最后相等的。
- 将必须相等的位置放在一起,相当于连一条边,而位置是点,这些点都是最后必须统一成一个字符的。这样S就被分成好几个连通分量。例如aabd z dfgh就会被分成3组:(a,a,g,h),(b,d,d,f)(z)每一组最终都是必须变成同一个字符。
- 分好组后就变得很清晰了,只要让每组变成同一个字符,使得变化的字符个数最少即可。那就是总字符数减去最多的那个字符的个数。例如(a,a,g,h)就需要4-2=2个变换,即g,h变成a,a不动就行。
- 最后将每一组的变换次数加起来就是最终的结果。
代码如下(java版):
package srm589;
public class SRM589DV1s250 {
private boolean[] visited = new boolean[50];
public int getmin(String S)
{
int result = 0;
for(int i=0;i<S.length();i++){
int[] count = new int[27];
dfs(i,S,count);
int total=0;
int max=0;
for(int j=0;j<count.length;j++){
total+=count[j];
if(max<count[j])
max = count[j];
}
result += total-max;
}
return result;
}
private void dfs(int k,String S,int[] count){
if(!visited[k]){
visited[k]=true;
count[S.charAt(k)-'a']++;
for(int i=0;i<S.length();i++){
if(S.charAt(i)==S.charAt(k)||i+k==S.length()-1){
dfs(i,S,count);
}
}
}
}
public static void main(String[] args) {
SRM589DV1s250 srm = new SRM589DV1s250();
System.out.println(srm.getmin("abaabb"));
}
}
SRM589 DV1 250 回文字符串的更多相关文章
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 回文字符串的判断!关于strlen(char * str)函数
#include <stdio.h> #include <string.h> int ishuiw(char * p); int main() { ;//true-false接 ...
- NYOJ_37.回文字符串 (附滚动数组)
时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba".当然,我们给你的问 ...
- 131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串
131. Palindrome Partitioning Given a string s, partition s such that every substring of the partitio ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- 【又见LCS】NYOJ-37 回文字符串
[题目链接] 回文字符串 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba& ...
- [NYOJ 37] 回文字符串
回文字符串 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba".当 ...
- 最长回文字符串(manacher算法)
偶然看见了人家的博客发现这么一个问题,研究了一下午, 才发现其中的奥妙.Stupid. 题目描述: 回文串就是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串. ...
- 1042 数字0-9的数量 1050 循环数组最大子段和 1062 序列中最大的数 1067 Bash游戏 V2 1092 回文字符串
1042 数字0-9的数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 给出一段区间a-b,统计这个区间内0-9出现的次数. 比如 10-19,1出现11次 ...
随机推荐
- 投票系统前台 C#,数据库SQL
------------vote.aspx.cs-------------------- private void Page_Load(object sender, System.EventArgs ...
- 转:30分钟掌握STL
三十分钟掌握STL 这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以化了两个晚上把它翻译出来.我没有对翻译出来的内容校验过.如果你没法在三十分钟内觉得有 ...
- Linux android studio :'tools.jar' seems to be not in Android Studio classpath.
问题: 'tools.jar' seems to be not in Android Studio classpath.Please ensure JAVA_HOME points to JDK ra ...
- [对话CTO]当当网熊长青:兴趣是成为优秀工程师的第一因素-CSDN.NET
Women Techmaker 北京站 [对话CTO]当当网熊长青:兴趣是成为优秀工程师的第一因素-CSDN.NET [对话CTO]当当网熊长青:兴趣是成为优秀工程师的第一因素 发表于2 ...
- A - Prime Ring Problem(素数环,深搜,打表)
Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into ...
- Android:Service的注意点以及一些知识点
1.自己练习service的start()方法开启一个service服务的时候,不管怎么开启按钮,就是开启不了service服务,控制台也没有报错信息, app不闪退,代码就那么几行.找了好久找不出来 ...
- PHP面向对象基础实例
<?phpclass marine{ public $blood = 50; //剩余的血 public $kills = 0; //杀敌数量 static $all_num = 0;//兵的数 ...
- CPU满格的元凶,这回是由于QTimer引起的(默认interval是0,太猛)
timer_space = new QTimer(); qDebug() << SystemGlobal::m_app->SpaceUse; qDebug() << ti ...
- python爬虫实战2百度贴吧爬html
转自:http://blog.csdn.net/wxg694175346/article/details/8927832 import string, urllib2 #定义百度函数 def baid ...
- java里,当long与上了int
long switchState = 0xf0000000000L; int result = (switchState & 0xff00000000L) > 0 ? 0x01 : 0x ...