CF 234 C Weather(粗暴方法)
2 seconds
256 megabytes
standard input
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.
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.
Print a single integer — the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.
6 3
ABBACC
2
ABCACA
3 2
BBB
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(粗暴方法)的更多相关文章
- 2018-01-28-TF源码做版本兼容的一个粗暴方法
layout: post title: 2018-01-28-TF源码做版本兼容的一个粗暴方法 key: 20180128 tags: IT AI TF modify_date: 2018-01-28 ...
- 【机器学习算法-python实现】协同过滤(cf)的三种方法实现
(转载请注明出处:http://blog.csdn.net/buptgshengod) 1.背景 协同过滤(collaborative filtering)是推荐系统经常使用的一种方法.c ...
- [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
- CF(协同过滤算法)
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
- 协同过滤(CF)算法
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
- C#入门篇6-3:字符串操作 string的ToString() Split()和Copy()方法
//ToString()方法 public static void OutPut() { //字符型转换 转为字符串 Console.WriteLine(.ToString("n" ...
- Eureka错误解决方法
# Eureka错误解决方法 ## security.basic.enabled 配置过时或不可用默认情况下:用户名:user密码:启动应用在控制台会输出,如下图: 也可以通过如下属性配置:sprin ...
- RunLoop 总结:RunLoop的应用场景(一)
参考资料 好的书籍都是值得反复看的,那好的文章,好的资料也值得我们反复看.我们在不同的阶段来相同的文章或资料或书籍都能有不同的收获,那它就是好文章,好书籍,好资料.关于iOS 中的RunLoop资料非 ...
- 挑子学习笔记:BIRCH层次聚类
转载请标明出处:http://www.cnblogs.com/tiaozistudy/p/6129425.html 本文是“挑子”在学习BIRCH算法过程中的笔记摘录,文中不乏一些个人理解,不当之处望 ...
随机推荐
- android 图片
1,setCompoundDrawables(Drawable left, Drawable top,Drawable right, Drawable bottom) 设置图片出现在textView, ...
- NFS和DHCP服务
1. NFS NFS,Network File System的简写,即网络文件系统.网络文件系统是FreeBSD支持的文件系统中的一种,也被称为NFS: NFS允许一个系统在网络上与他人共享目录和文件 ...
- linux 05
日期与时间命令:date.日历的命令:cal.计算器:bc 要使用quit退出 在命令行模式里执行命令时,会有两种主要情况: 一种是该命令会直接显示结果然后回到命令提示符等待下一个命令的输入 dat ...
- 不同子系统采用不同MySQL编码LATIN1和UTF8的兼容
程序处理 这是一个历史遗留系统, 旧的系统是C++开发的, 插入数据的时候, 没有统一MYSQL各个层次(服务器, 数据库, 表, 列)的编码, 这个情况基本上是MYSQL的默认安装导致的, 实际的数 ...
- Python_编程题集_003_递归算法解析
3.递归算法: 3)传入一个Json串,返回一个字典,字典只取出Json最底层的数据, 中间如果有字符串也要进行处理,请以下面的数据为例,请用递归方法实现 Json:{"a":&q ...
- tornado框架基础07-sqlalchemy查询
01 查询结果 上节使用query从数据库中查询到了结果,但是query返回的对象是直接可用的吗? 首先导入模块 from connect import session from user_modul ...
- STM32定时器的两个小难点
TIM1 TIM8 挂在APB2上 一般为72M 也即APB2分频系数为1其余TIMER可以认为都挂在APB1上,一般为36M 也即APB1分频系数为2 或者更大 至少为2 APB1不能超过36M定时 ...
- 大数据学习——mapreduce运营商日志增强
需求 1.对原始json数据进行解析,变成普通文本数据 2.求出每个人评分最高的3部电影 3.求出被评分次数最多的3部电影 数据 https://pan.baidu.com/s/1gPsQXVYSQE ...
- xtu summer individual-4 B - Party All the Time
Party All the Time Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Orig ...
- asp.net 页面缓存、数据缓存
页面缓存,webform框架的aspx页面,服务器引擎生成的页面可以被缓存.