Codeforces219c-color stripe DP + greedy

Source: Internet
Author: User

Meaning: N, K, which is a string that has been filled with N colors. You can select K colors, ask how many boxes you need to change at least to make the colors of adjacent boxes different.

Solution: When k = 2, we discuss it separately. We cannot use greed. We can use it in other cases.

Solution code:

1 // file name: 219c. CPP 2 // Author: darkdream 3 // created time: saturday, July 26, 2014, 56 seconds, 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 # InCl Ude <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", & 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 E LSE tempb ++; 44} else {45 if (STR [I] = 'B') 46 Tempa ++; 47 else tempb ++; 48} 49} 50 if (Tempa <tempb) 51 {52 printf ("% d \ n", Tempa); 53 for (INT I = 0; I <Len; I ++) 54 {55 if (I % 2 = 0) 56 printf ("B"); 57 else printf (""); 58} 59} else {60 printf ("% d \ n", 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 \ n", sum); 100 puts (STR); 101 return 0; 102}
View code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.