zoukankan      html  css  js  c++  java
  • codeforces219C

    题意:给你n,k,大意就是说给你一个已经涂满颜色长为n的字符串,现有k种颜色可以选择,问你最少要改变多少个箱子的颜色使得相邻箱子之间颜色不同。

    解题思路:当k = 2 时单独讨论,不能用贪心,其余情况都可贪心得到。

    解题代码:

      1 // File Name: 219c.cpp
      2 // Author: darkdream
      3 // Created Time: 2014年07月26日 星期六 15时45分56秒
      4 
      5 #include<vector>
      6 #include<list>
      7 #include<map>
      8 #include<set>
      9 #include<deque>
     10 #include<stack>
     11 #include<bitset>
     12 #include<algorithm>
     13 #include<functional>
     14 #include<numeric>
     15 #include<utility>
     16 #include<sstream>
     17 #include<iostream>
     18 #include<iomanip>
     19 #include<cstdio>
     20 #include<cmath>
     21 #include<cstdlib>
     22 #include<cstring>
     23 #include<ctime>
     24 
     25 using namespace std;
     26 char str[1000000];
     27 int main(){
     28     int n , k ; 
     29     scanf("%d %d",&n,&k);
     30     scanf("%s",str);
     31     int len = strlen(str);
     32     int sum  = 0 ;
     33     if(k == 2)
     34     {
     35        int tempa,tempb;;
     36        tempa = tempb = 0 ; 
     37        for(int i = 0 ;i < len ;i ++)
     38        {
     39           if(i %2 == 0)
     40           {
     41               if(str[i] == 'A')
     42                   tempa ++;
     43               else tempb ++;
     44           }else {
     45              if(str[i] == 'B')
     46                  tempa ++ ;
     47              else tempb ++ ;
     48           }
     49        }
     50        if(tempa < tempb)
     51        {
     52           printf("%d
    ",tempa);
     53           for(int i = 0 ;i < len ;i ++)
     54           {
     55             if(i % 2 == 0 )
     56                 printf("B");
     57             else printf("A");
     58           }
     59        }else{
     60           printf("%d
    ",tempb);
     61           for(int i = 0 ;i < len ;i ++)
     62           {
     63             if(i % 2 == 0 )
     64                 printf("A");
     65             else printf("B");
     66           }
     67        
     68        }
     69        return 0 ; 
     70     }
     71     for(int i = 1 ;i < len ;i ++)
     72     {
     73        if(str[i] == str[i-1])
     74        {
     75            int ok = 0 ; 
     76            for(int j = 0 ;j < k ;j ++)
     77            {
     78               char t = j + 'A';
     79               if(t != str[i-1] && t != str[i+1] )
     80               {
     81                  str[i] = t ;
     82                  ok = 1; 
     83                  break;
     84               }
     85            }
     86            if(ok == 0 )
     87            for(int j = 0 ;j < k ;j ++)
     88            {
     89              char t = j +'A';
     90              if(t != str[i-1])
     91              {
     92                str[i] = t; 
     93                break;
     94              }
     95            }
     96            sum ++ ; 
     97        }
     98     }
     99     printf("%d
    ",sum);
    100     puts(str);
    101 return 0;
    102 }
    View Code
    没有梦想,何谈远方
  • 相关阅读:
    使用JavaScript获取select元素选中的value和text
    EF应用一:Code First模式
    EF常用查询语句
    在EF中执行SQL语句
    c++学习笔记之继承篇
    pyqt系列原创入门教程
    python sorted排序用法详解
    常见排序算法-Python实现
    作品集
    deepin系统如何安装deb格式的软件
  • 原文地址:https://www.cnblogs.com/zyue/p/3870272.html
Copyright © 2011-2022 走看看