UVA – 11103 – WFF ‘N PROOF   Leave a comment


and another boring problem 😀

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class WFF {

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		while (true) {
			char[] s = in.readLine().toCharArray();
			if (s.length == 1 && s[0] == '0')
				return;
			String res = "";

			int cnt = 0;
			for (int i = 0; i < s.length; i++) {
				if (Character.isLowerCase(s[i])) {
					res = s[i] + res;
					s[i] = 0;
					cnt++;
					break;
				}
			}
			for (int i = 0; i < s.length; i++)
				if (s[i] == 'N') {
					res = 'N' + res;
					s[i] = 0;
				}
			while (true) {
				boolean flag = true;

				for (int i = 0; i < s.length; i++) {
					if (Character.isLowerCase(s[i]) && cnt < 2) {
						res = s[i] + res;
						s[i] = 0;
						cnt++;
						flag = false;
					} else if (Character.isUpperCase(s[i])) {
						if (s[i] == 'N' && cnt == 1) {
							res = s[i] + res;
							s[i] = 0;
							flag = false;
						} else if (cnt == 2 && s[i] != 'N') {
							res = s[i] + res;
							s[i] = 0;
							flag = false;
							cnt = 1;
						}
					}
				}
				if (flag)
					break;
			}
			if (cnt == 2) {
				System.out.println(res.substring(1, res.length()));
			} else if (res.length() > 0 && cnt == 1)
				System.out.println(res);
			else
				System.out.println("no WFF possible");
		}
	}
}

Advertisements

Posted July 5, 2012 by epicrado in Greedy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: