×

Author: Alei Reyes
Primary Tester: Hussain Kara Fallah
Secondary Tester: Kacper Walentynowicz
Editorialist: Hussain Kara Fallah

Cakewalk

None

# PROBLEM:

Given a string consisting of characters U,D. In a move you can select a consecutive substring and flip all characters belonging to. (U changes to D and vice versa). What's the minimum number of moves to make all characters equal?

# EXPLANATION:

The first thing that will come up to our minds is that we should split the string from the beginning into a block of consecutive 'U' characters, followed immediately by a block of consecutive 'D' characters, then a block of 'U' characters, then a block of 'D' characters... etc. (Of course if our string starts with D then the letters are the opposite). And flip the D blocks.

The number of D blocks = (The number of U blocks) or (the number of U blocks + 1)

If our string starts with D then

The number of U blocks = (The number of D blocks) or (the number of D blocks + 1)

so our answer would be [number of blocks / 2] (of course rounded down).

# Example:

Consider our string was "UUUDDUUUDDDUU"

We would split it into :

[UUU] , [DD] , [UUU] , [DDD] , [UU]

The best solution is to flip [DD] , [DDD]

# Another Example :

Consider our string was "DDDUDDUU"

We would split it into :

[DDD] , [U] , [DD] , [UU]

Here because the number of U blocks is equal to number of D blocks then flipping the blocks of any letter would be optimal.

Why is this always correct?

If we are looking for a better solution than the mentioned above, then we should flip at least 2 of our targeted blocks at once. But that's impossible because we would affect at least one proper block between them (consisting of characters equal to our final character) and we must return it to its original state again and that's impossible without an additional move.

Consider we tried to obtain a better strategy in the first example:

That means that we would change all D letters to U in one move. It's only possible by flipping the substring [DDUUUDDD] (or any substring formed by extending this one from left or right), but doing this would change U letters to D and we need to return them back to U, and this will cost us at least one operation.

# AUTHOR'S AND TESTER'S SOLUTIONS:

AUTHOR's solution: Here
TESTER's solution: Here

This question is marked "community wiki".

1181134
accept rate: 0%

19.8k350498541

3

Useless tutorials are here but important ones are not :(

(19 Jun '17, 16:19) 4★
1

(19 Jun '17, 17:49)
7

At some point of time in past, these tutorials wouldn't have been that useless for you mate.

(19 Jun '17, 18:23) 4★

 0  #include int flip(char *a, char c) { int i,count=0; for(i=0;a[i]!='\0';i++) { if(a[i]==c) { while(a[++i]==c); ++count; } } return count; } int main() { char a[50]; int t,u,d; scanf("%d",&t); while(t--) { scanf("%s",&a); u=flip(a,'U'); d=flip(a,'D'); printf("%d",u>d?d:u); } return 0; }  Runs perfectly on my machine but not accepted by codechef. Saying wrong answer. answered 21 Jun '17, 22:09 1★harcasm 1 accept rate: 0% You dont take strings with"&' character. Try making it ("%s",a) in scanf. Also, make sure your string length is ATLEAST 1 MORE than given in cosntraints. Else '\0' wont be stored in it, resulting in infinite loop/undefined behaviour. (21 Jun '17, 23:36)
 0 @Vijju123 My code is also seems working correct.But not accepted in code chef :( class Ada_and_crayons { private static InputReader in = new InputReader(System.in); public static void main(String[] args)throws Exception { int T= in.readInt(); StringBuilder sb = new StringBuilder(); while(T-- >0) { String str = in.readString(); char chr[] = str.toCharArray(); int U_count=0; int D_count=0; for(int i =0;i
 0 What is the problem with my code https://www.codechef.com/viewsolution/14621807 answered 20 Jul '17, 14:27 11●1 accept rate: 0%

my code is=

# include <iostream>

using namespace std;

int main() {int t; cin>>t; while(t>0) {char s[100]; int i,p=0,c1=0,c2=0; cin>>s; for(i=0;i!='\n';i++) { if(s[i]=='U') { if(p==1) {continue;} else { c1++; p=1; } } else if(s[i]=='D') { if(p==2) {continue; } else { c2++; p=2; } } } if(c1>=c2){cout<<c2<<endl;} else {cout<<c1<<endl;} t--; } return 0; }

again and again it is showing wrong answer but in my machine it is working fine

32
accept rate: 0%

 -1 useless. Even couldn't explain simple thing answered 19 Jun '17, 21:47 -3 accept rate: 0% 4 Well even simple problems need proofs. and proofs are usually harder than the problem. If I asked are prime numbers infinite you would tell yes because it's clear that they are. But can you prove that easily? Proofs are necessary for even simple problems, if this problem seems very easy for you and I am making it more complicated then you can skip them, but just saying that you need to do this without a proof is not ideal (even if the proof is complicated) if you really think it's useless you should say why and what can we fix? not just throw a comment. (21 Jun '17, 22:42) 1 I agree with @deadwing97 . Its easy to throw tantrums. If you really want something to fix, then suggest something. OR atleast, bring it up POLITELY. You know, never hurts being nice ^^ (21 Jun '17, 23:33)
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×15,851
×1,688
×74
×8

question asked: 19 Jun '17, 03:34

question was seen: 2,103 times

last updated: 13 Mar '18, 19:58