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. 洛谷 P1085 不高兴的津津

    这道题就是经典的条件分支的题https://www.luogu.org/problemnew/show/P1085 code: #include <stdio.h> int main() ...

  2. 洛谷P1421 小玉买文具

    这道题其实就是编程最基础的逻辑,没什么好讲的输入,输出就完了,非常简单! code: #include<cstdio> #include<iostream> using nam ...

  3. PHP+Apache2.x+phpMyAdmin安装和配置

    1>各个部件的下载 目前在windows下开发 PHP官网下载地址:https://windows.php.net/download PHP有TS(ThreadSafe)和NTS两个版本,所以按 ...

  4. 12. KEY_COLUMN_USAGE

    12. KEY_COLUMN_USAGE KEY_COLUMN_USAGE表描述哪些键列具有约束. KEY_COLUMN_USAGE表有以下列: CONSTRAINT_CATALOG :约束所属目录的 ...

  5. RPM Package Manager

    本文大部分内容来自鸟哥的Linux私房菜,并且由作者根据自己的学习情况做了一些更改,鸟哥原文链接 1. 程序的安装方式 源代码安装:利用厂商释出的Tarball 来进行软件的安装,每次安装程序都需要检 ...

  6. Wall Treatment

    * wall treatment You can combine the turbulent flow interfaces with different types of wall treatmen ...

  7. Cisco路由器交换机基础配置

    交换机配置 交换机基本状态: switch: # ROM状态, 路由器是rommon> hostname> # 用户模式 hostname# # 特权模式 hostname(config) ...

  8. [第一波模拟\day3\T3]{益智游戏}(game.cpp)

    [问题描述] 小P和小R在玩一款益智游戏.游戏在一个正权有向图上进行. 小P控制的角色要从A点走最短路到B点,小R控制的角色要从C点走最短路到D点. 一个玩家每回合可以有两种选择,移动到一个相邻节点或 ...

  9. Python requests.post方法中data与json参数区别

    在通过requests.post()进行POST请求时,传入报文的参数有两个,一个是data,一个是json. data与json既可以是str类型,也可以是dict类型. 区别: 1.不管json是 ...

  10. python接口自动化-multipart/form-data上传图片

    前言 在提交表单操作的时候,经常会遇到图片上传的操作,图片上传是一个单独的接口,本篇以禅道为例,介绍如何上传图片 上传接口 1.以禅道上提交bug为例,在选择图片时,点确定按钮,就是上传图片了 2.用 ...