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. 用固定长度的数组实现stack queue

    package my_basic.class_3; /** * 用数组结构实现大小固定的队列和栈 */ public class Code_01_Array_stack_queue { public ...

  2. hdu 6441 Find Integer(费马大定理+勾股数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6441(本题来源于2018年中国大学生程序设计竞赛网络选拔赛) 题意:输入n和a,求满足等式a^n+b^ ...

  3. 「 Luogu P2574 」 XOR的艺术——线段树

    # 解题思路 这题不难,但是原谅我一开始的傻逼想法,一会儿再给大家透露透露. 先说怎么做这题. 显然对于 $0$ 和 $1$ 来说,异或无非也就只有两种变化 异或了奇数次,$0$ 就会变成 $1$,$ ...

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

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

  5. JavaScript中的内置函数

    JavaScript中的内置函数 制作人:全心全意 在使用JavaScript语言时,除了可以自定义函数之外,还可以使用JavaScript的内置函数,这些内置函数是由JavaScript语言自身提供 ...

  6. webpack 之 loader

    loader简介 loader在webpack.config.js中进行配置,配置之后,会自动检测打包过程中引入的文件(import或require),通过test成功匹配被引入的文件名后,会对文件内 ...

  7. ubuntu卸载编译安装的软件

    cd 源代码目录 make clean ./configure make make uninstall

  8. json_decode 解析带BOM头文件错误

    //取前三个字符 并转化为ASCII 判断是否为BOM文件 $charset[1] = substr($result, 0, 1); $charset[2] = substr($result, 1, ...

  9. Django框架基础知识12-中间件及上下文处理器

    Django中间件(Middleware) 是一个轻量级.底层的“插件”系统,可以介入Django的请求和响应处理过程,修改Django的输入或输出. django 中的中间件(middleware) ...

  10. POJ 3621 Sightseeing Cows (最优比率环 01分数划分)

    题意: 给定L个点, P条边的有向图, 每个点有一个价值, 但只在第一经过获得, 每条边有一个花费, 每次经过都要付出这个花费, 在图中找出一个环, 使得价值之和/花费之和 最大 分析: 这道题其实并 ...