C. Color Stripe
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors.
Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to
repaint the cells.

Input

The first input line contains two integers n and k (1 ≤ n ≤ 5·105; 2 ≤ k ≤ 26).
The second line contains n uppercase English letters. Letter "A"
stands for the first color, letter "B" stands for the second color and so on. The first k English
letters may be used. Each letter represents the color of the corresponding cell of the stripe.

Output

Print a single integer — the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.

Sample test(s)
input
6 3
ABBACC
output
2
ABCACA
input
3 2
BBB
output
1
BAB

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map> #define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1) #define bug printf("hihi\n") #define eps 1e-8
typedef __int64 ll;
#define INF 0x3f3f3f3f using namespace std; int dp[500001][27];
int pre[500001][27];
int n,k;
char c[500005]; void show(int pos,int i)
{
vector<char>ans;
ans.clear();
while(pos!=-1)
{
ans.push_back(i+'A');
i=pre[pos][i];
pos--;
}
for(i=ans.size()-1;i>=0;i--)
printf("%c",ans[i]);
printf("\n");
} int main()
{
int i,j;
int kk;
// cout<<500000*26*26<<endl;
while(~scanf("%d%d",&n,&kk))
{
memset(dp,INF,sizeof(dp));
scanf("%s",c);
for(i=0;i<26;i++)
dp[0][i]=pre[0][i]=1;
dp[0][c[0]-'A']=0;
for(i=1;i<n;i++) //500000*26*26的复杂度能够在2秒内跑完预计仅仅有CF的机子敢试试了
for(j=0;j<kk;j++)
for(k=0;k<kk;k++)
{
if(j==k) continue;
int t=1;
if(k==c[i]-'A') t=0;
if(dp[i-1][j]+t<dp[i][k])
{
dp[i][k]=dp[i-1][j]+t;
pre[i][k]=j;
}
}
i=0;
for(j=1;j<kk;j++)
if(dp[n-1][j]<dp[n-1][i]) i=j;
printf("%d\n",dp[n-1][i]);
show(n-1,i);
}
return 0;
}

CF 234 C Weather(粗暴方法)的更多相关文章

  1. 2018-01-28-TF源码做版本兼容的一个粗暴方法

    layout: post title: 2018-01-28-TF源码做版本兼容的一个粗暴方法 key: 20180128 tags: IT AI TF modify_date: 2018-01-28 ...

  2. 【机器学习算法-python实现】协同过滤(cf)的三种方法实现

    (转载请注明出处:http://blog.csdn.net/buptgshengod) 1.背景       协同过滤(collaborative filtering)是推荐系统经常使用的一种方法.c ...

  3. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  4. CF(协同过滤算法)

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  5. 协同过滤(CF)算法

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  6. C#入门篇6-3:字符串操作 string的ToString() Split()和Copy()方法

    //ToString()方法 public static void OutPut() { //字符型转换 转为字符串 Console.WriteLine(.ToString("n" ...

  7. Eureka错误解决方法

    # Eureka错误解决方法 ## security.basic.enabled 配置过时或不可用默认情况下:用户名:user密码:启动应用在控制台会输出,如下图: 也可以通过如下属性配置:sprin ...

  8. RunLoop 总结:RunLoop的应用场景(一)

    参考资料 好的书籍都是值得反复看的,那好的文章,好的资料也值得我们反复看.我们在不同的阶段来相同的文章或资料或书籍都能有不同的收获,那它就是好文章,好书籍,好资料.关于iOS 中的RunLoop资料非 ...

  9. 挑子学习笔记:BIRCH层次聚类

    转载请标明出处:http://www.cnblogs.com/tiaozistudy/p/6129425.html 本文是“挑子”在学习BIRCH算法过程中的笔记摘录,文中不乏一些个人理解,不当之处望 ...

随机推荐

  1. tmp note

    cat file.txt > /dev/null 2>&1 丢弃错误和标准输出 systemctl isolate multi-user.target 切换回多用户命令行模式 sy ...

  2. 谈谈你对java的理解

    这个题目是考察多个方面 但是要回答出关键点: 1.平台无关性 2.GC 3.语言特性.泛型.反射.lamda 4.面向对象 5.类库 6.异常处理

  3. struts2中关于jsp页面向action传值出现乱码问题

    在JSP页面输入中文并传给后台的时候,常常会出现乱码问题,产生乱码的原因:java在进行传值的时候,默认用的是iso-8859-1的编码形式进行传输,而我们jsp页面常用的则是utf-8的编码形式.所 ...

  4. UITextView与UITextfield的区别

    IOS中的UITextView和UITextField都是文本输入控件并都能够调用系统键盘.本次特酷把介绍UITextView和UITextField的区别.简单来说,UITextView和UITex ...

  5. 前端打包--source-map=false作用

    参考:http://www.cnblogs.com/axl234/p/6500534.htmlng serve默认会产生.map文件,该文件保存有原始代码与运行代码的映射关系, 浏览器可以通过它找到原 ...

  6. spring注解开发-AOP(含原理)

    一:AOP基本使用 AOP指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式: 步骤一:导入aop模块:Spring AOP:(spring-aspects) <depen ...

  7. mysql查询表中最小可用id值

    今天在看实验室的项目时,碰到的一个问题,.先把sql语句扔出来 // 这条语句在id没有1时,不能得到正确的查询结果. select min(id+1) from oslist c where not ...

  8. SQLServer__问题记录

    “备份集中的数据库备份与现有的xx数据库不同”  参考链接: http://www.cnblogs.com/huangfr/archive/2012/08/09/2629687.html RESTOR ...

  9. Java学习之for循环打印菱形练习

    for循环语句是Java程序设计中非常有用的循环语句.一个for循环可以用来重复执行某条语句,直到某个条件得到满足.在Java 5新增的加强的foreach语法,也非常有用. 1. for语句 for ...

  10. Android ScaleDrawable

    顾名思义,Android ScaleDrawable实现一个drawable的缩放.写一个例子. 一个线性布局,垂直放几个ImageView,然后依次缩放若干个ScaleDrawable. 布局文件: ...