1. 程式人生 > >[字首標記] [POJ] 3276 Face The Right Way

[字首標記] [POJ] 3276 Face The Right Way

陣列開太大

memset超時好幾發

N方列舉+字首標記

一頓操作

/*
    Zeolim - An AC a day keeps the bug away
*/

//pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <sstream>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
typedef long long ll;

const int MAXN = 5e3 + 10;

int arr[MAXN];

int mark[MAXN] = {0};

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);     cout.tie(0);
    //freopen("D://test.in", "r", stdin);
    //freopen("D://test.out", "w", stdout);
    
    int n;

    char t;

    scanf("%d", &n);
	
	getchar();
	
    for(int i = 1; i <= n; i++)
    {
        scanf("%c",&t);
        getchar();
        arr[i] = (t == 'B');
    }

    int ans = 0x3f3f3f3f, step = 0;
	
//	for(int i = 0; i <= n; i++)
//	{
//		cout<<arr[i]<<' ';
//	}
    for(int i = 1; i <= n; i++)
    {
    	memset(mark, 0, sizeof(mark));
		 
        int sum = 0, tim = 0;

        for(int j = 1; j <= n; j++)
        {
            sum += mark[j];

            if(arr[j] + sum % 2 == 1)
            {
                mark[j] = 1;
                sum += mark[j];

                if(j + i > n + 1)
                    goto l1;

                mark[j + i] = -1;

                tim++;
            }
        }
        if(tim < ans)
        {
        	ans = tim, step = i;
		}

        l1:
        	
        continue;

    }

    cout<<step<<' '<<ans<<endl;

    return 0;
}