Member SRM 474

コードだけ。講義中だったのと、MediumのN+1にすべきところをNにしていて気づかなかったのが敗因。

PalindromesCount

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define FOR(i,s,e) for (int i = int(s); i != int(e); i++)
#define FORIT(i,c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ISEQ(c) (c).begin(), (c).end()

class PalindromesCount {
private:
	bool checkpalin(string str){
		int a = 0;
		int b = (int)str.size() -1;
		bool check = true;
		while(a<=b){
			if(str[a] != str[b]){
				check = false;
				break;
			}
			a++;
			b--;
		}
		return check;
	}


public:
	int count(string A, string B) {
		size_t pos = 0;
		int result = 0;
		int size = (int)A.size();
		string Acopy;
		for(int i=0; i<size+1; i++){
			Acopy = A;
			Acopy.insert(pos, B);
			if(checkpalin(Acopy)) result++;
			pos++;
		}

		return result;
	}

};

RouteIntersection

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define FOR(i,s,e) for (int i = int(s); i != int(e); i++)
#define FORIT(i,c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ISEQ(c) (c).begin(), (c).end()

class RouteIntersection {
private:
	bool checkvalid(const int N, vector<int> coords, const string moves, int i, int j){
		bool check = true;
		for(int l=i; l<j && check;l++){
			if(coords[l] == N+1) continue;
			bool check2 = false;
			for(int k=l+1; k<=j && !check2 ; k++){
				if(coords[l] == coords[k] && moves[l] != moves[k]){
					coords[l] = N+1;
					coords[k] = N+1;
					check2 = true;
				}
			}

			if(!check2){
				if(coords[l] == coords[i-1] && moves[l] != moves[i-1]){
					coords[l] = N+1;
					coords[i-1] = N+1;
					return checkvalid(N, coords, moves,i-1,j);
				}else if(coords[l] == coords[j+1] && moves[l] != moves[j+1]){
					coords[l] = N+1;
					coords[j+1] = N+1;
					return checkvalid(N, coords, moves, i, j+1);
				}else{
					check=false;
				}
			}
		}

		return check;
	}

public:
	string isValid(int N, vector<int> coords, string moves) {
		bool result = true;
		int size = (int)coords.size();

		for(int i=0; i<size-1 && result; i++){
			for(int j=i+1; j<size; j++){
				if(coords[i] == coords[j] && moves[i] != moves[j]){
					if(checkvalid(N,coords,moves,i,j)){
						result = false;
						break;
					}
				}
			}
		}

		if(result){
			return "VALID";
		}else{
			return "NOT VALID";
		}
	}

};