blackhat132's blog

By blackhat132, history, 8 years ago, In English

This is the link to my submission: http://codeforces.com/contest/131/submission/20141956

This is the problem: http://codeforces.com/problemset/problem/131/A

when i run it on my machine againt "cAPSlock" i get "cAPSlock" as the answer. But the submission tells me it gets the output "Capslock"

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
8 years ago, # |
  Vote: I like it +3 Vote: I do not like it

First, the input for which your answer was wrong is not "cAPSlock", it is "cAPSlOCK". The difference is that there is a lowercase 'l' between two uppercase substrings, which breaks your code:

		bool change=false;
		for (int i=1; i<str.length(); ++i) {
			if (str.at(i)-96 < 0) {
				change=true;
				for (int j=i; j<str.length(); ++j)
					if (str.at(j)-96 > 0) {
						change=false;
					}
			}
		}

Note that you scan the string looking for an uppercase character. When you find one, you set the flag as true, and then scan the remaining string for a lowercase character. Then, if you find a lowercase character, you reset the flag to false. The problem is, you continue to scan the string. And when you do this, you "undo" what you had done in the previous iteration (you "forget" what you saw). Another thing you code does wrong: when the change flag is true, you set the first character to uppercase and the rest to lowercase, which is not what the problem asks you to do. It asks you to change every character case. So if the string is completely uppercase, the result should be completely lowercase. What I advise you to do: think of a different way to check whether the string matches the restriction or not. You don't need to iterate through it twice as your code does. You can set a flag as true, and iterate through it only once, starting at position 1. If you happen to see a lowercase character, set the flag as false. At the end of the loop, if your flag is still true, it means there wasn't any lowercase characters — so you are allowed to flip the casing. If the flag is false, then there was at least one lowercase character in the middle — then you shouldn't do anything. Here is my answer for this problem:

Code