ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Broken Necklace /c++
    ALGORITHM/USACO 2019. 8. 11. 07:24
    728x90

    진짜 쉬운 문제라고 생각하고 간단하게 변수를 짜다보니 의식의 흐름대로 코드가 개판이 났다.

    어서빨리 변수명을 짓는 관습부터 챙겨놔야겠다.

    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main() {
    	ofstream fout("beads.out");
    	ifstream fin("beads.in");
    	int N;
    	string s1, s2;
    	fin >> N;
    	fin >> s1;
    	s2 = s1;
    	s1 += s2;
    	int answer = 0;
    	int lalatest = 0;
    	for (int i = 0; i < N; i++) {
    		int j = i;
    		char start = s1[i];
    		int latest = 0;
    		int k = 0;
    		bool first = true;
    		int next = 0;
    		int a = 0;
    		int try1 = 0;
    		for (int i = 1; i < N; i++) {
    			if (s1[i] == s1[i - 1]){
    				try1++;
    			}
    			if (try1 == N - 1) {
    				fout << (try1 + 1) << endl;
    				return 0;
    			}
    		}
    
    		while (j < 2 * N) {
    			if (first) {
    				if (start != s1[j]) {
    					if (a == 0)
    						next = k;
    					a = 1;
    				}
    				if (start == 'w' && s1[j] != 'w')
    					start = s1[j];
    				if (s1[j] == 'w' || s1[j] == start) {
    					j++;
    					latest++;
    					k++;
    				}
    				else {
    					lalatest = latest;
    					latest = 0;
    					start = s1[j];
    					if (next != 0)
    						i += next - 1;
    					else
    						i += k - 1;
    					first = false;
    				}
    			}
    			else {
    				if (s1[j] == 'w' || s1[j] == start) {
    					j++;
    					latest++;
    				}
    				else {
    					if (answer < latest + lalatest) {
    						answer = latest + lalatest;
    					}
    					lalatest = latest;
    					first = false;
    					break;
    				}
    			}
    		}
    	}
    	if (N < answer) {
    		fout << N << endl;
    	}else
    		fout << answer<<endl;
    	return 0;
    }

    'ALGORITHM > USACO' 카테고리의 다른 글

    Solving the usaco transform problem in c++  (0) 2019.08.11
    Milking Cows /c++  (0) 2019.08.11
    Friday the Thirteenth /c++  (0) 2019.08.11
    Task 'gift1': Greedy Gift Givers /c++  (0) 2019.08.11
    your ride is here c++  (0) 2019.08.11

    댓글

Designed by Tistory.