SRM532 Div2 600

本番で解けなかった600問題を解いた。

#line 5 "DengklekMakingChains.cpp"
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
class DengklekMakingChains {
public:
	int maxBeauty(vector <string> chains) {
        
        int full_max = 0,single_max = 0,side_max = 0;
        vector<string> side;
        bool flag = true;
        
        for (int i=0; i < chains.size(); i++) {
            if (chains[i][0] != '.' && chains[i][1] != '.' && chains[i][2] != '.') {
                int max = (int)(chains[i][0] - '0') + (int)(chains[i][1] - '0') + (int)(chains[i][2] - '0');
                full_max += max;
            }else if (chains[i][0] == '.' && chains[i][1] != '.' && chains[i][2] == '.') {
                int single = (int)(chains[i][1] - '0');
                if(single_max < single) {
					single_max = single;
				}
            }else if (chains[i][0] != '.' || chains[i][2] != '.') {
                side.push_back(chains[i]);
                flag = false;
            }
        }
		
        if (flag) {
            return max(single_max,(side_max + full_max));
        }
		
		
		if(side.size() == 1) {
			vector<int> num;
			
			num.push_back(0);
			int count = 0;
			for (int n=0; n < side[0].size(); n++) {
				if (side[0][n] != '.') {
					num[count] += (int)(side[0][n] - '0');
				}else {
					count++;
					num.push_back(0);
				}
			}
			
			vector<int>::iterator it = max_element( num.begin(), num.end() );
			if (side_max < *it) {
				side_max = *it;
			}
			
			return max(single_max,(side_max + full_max));
		}
		
		
        for (int i=0; i < side.size() - 1; i++) {
            for (int j=i+1; j < side.size(); j++) {
                for (int k=0; k < 2; k++) {
					string join = ""; 
                    if (k == 0) {
                        join = side[i] + side[j];
                    }else {
                        join = side[j] + side[i];
                    }
					vector<int> num;
					num.push_back(0);
					int count = 0;
					for (int n=0; n < join.size(); n++) {
						if (join[n] != '.') {
							num[count] += (int)(join[n] - '0');
						}else {
							count++;
							num.push_back(0);
						}
					}
					
					vector<int>::iterator it = max_element( num.begin(), num.end() );
					if (side_max < *it) {
						side_max = *it;
					}
                }
            }
        }
		
        
		return max(single_max,(side_max + full_max));
	}
    
};
広告を非表示にする