Monday, February 17, 2014

Rockethon 2014 problem B - Word Folding solution

Rockethon 2014 problem B - Word Folding :  http://codeforces.com/contest/391/problem/B

Rockethon 2014 problem B - Word Folding solution :  http://ideone.com/43qAoS

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main() {
    int a[1005], max;
    char s[1005];
    scanf("%s\n", s);
    memset(a, 0, sizeof(a));
    max=0;
    for(int i=0; i<strlen(s); i++) {
        for(int j=i-1; j>=0; j-=2) {
            if(s[i]==s[j] && a[j]+1>a[i]) a[i]=a[j]+1;
        }
        if(a[i]>max) max=a[i];
    }
    printf("%d\n", max+1);
    return 0;
}

note: during contest i couldnt solve it, so after contest i glanced AC codes of tourist and rng_58, i liked both. well, use dynamic programming, i mean calculate only once. loop i from 0 to strlen(s), and j from i-1 to 0. however, decrement j by 2(j-=2), because when folding the string, symmetrical representation of i goes back by 2. and update maximum resemblance (if(a[i]>max) max=a[i];)

1 comment: