2

I'm trying to solve a question from kattis as shown here regarding string factorisation. I've tried adjusting my code for quite abit but it still seems theoretically correct. Not sure why it still fails for some of the test cases.

#include <stdio.h>
#include <iostream>
#include <string>
#include <map>
#include <algorithm>

using namespace std;

string shortener (string input) {
    map<string, int> freq;
    int flag = 0;
    for (int i = (input.length())/2; i >= 1 && !flag; i--) {
        for (int d = 0; d + i + i <= input.length(); d++) {
            if (input.substr(d, i) == input.substr(d + i, i)) {

                freq[input.substr(d,i)]++;
                flag = 1; // stop at this size
            }
        }
    }

    int largest = 0;
    if (freq.empty()) return input;

    //string largest;
    auto x = max_element(freq.begin(), freq.end(),
                         [](const pair<string, int>& p1, const pair<string, int>& p2) {
                             return p1.second < p2.second; });
    if (x->first == input) return input;

    int a = input.find(x->first);

    for (int i = 0; i < x->second ; i++) {
        input.replace(a, x->first.length(), "");
        a = input.find(x->first);
    }
    if (a != -1) {
        if (!input.substr(0, a).empty())
            input.replace(0, a, shortener(input.substr(0, a)));
        if (!input.substr(a + x->first.length()-1, input.length()-1).empty())
            input.replace(a + x->first.length()-1, input.length()-1, shortener(input.substr(a + x->first.length()-1, input.length()-1)));
        //cout << input.substr(a + x->first.length()-1, input.length()-1) << endl;
        input.replace(a, x->first.length(), shortener(x->first));
    }

    return input;
}

int main () {
    string input;
    cin >> input;

    cout << shortener(input).length() << endl;
}

I know my code may not be the most efficient, but I'm hoping to find out what kind of test case might potentially break my code.

lws803
  • 907
  • 2
  • 10
  • 21
  • 2
    And what test cases is your code failing exactly, and why? Post a [MCVE] please! –  Feb 13 '18 at 19:16
  • Wait! Is it you want to draw us into one of these useless black hole, maelstrom of a time waste code judge engine? Nope, I' m not gonna do that for you, it's your doom alone. –  Feb 13 '18 at 19:20
  • "Not sure why it still fails for some of the test cases." Because you missed some conditions somewhere. Find out what circumstances cause it to fail and come back or take advantage of this new knowledge and fix it yourself. – user4581301 Feb 13 '18 at 19:40
  • *I've tried adjusting my code for quite abit but it still seems theoretically correct.* -- Throwing stuff onto a wall until something sticks is not a way to write programs. That's why these "online judge" questions are a waste of time if you yourself do not have the test cases, and instead rely on some mystery people behind the curtain to determine if your code is ok or not. – PaulMcKenzie Feb 13 '18 at 19:46
  • Pay no attention to the man behind the curtain! Come back tomorrow! – user4581301 Feb 13 '18 at 19:52
  • So the normal request is, please write my code for me, this one seems to be please test my code for me. I find this vastly less fulfilling. – Jonathan Mee Feb 13 '18 at 21:23
  • How many test cases have you tried? – stark Feb 13 '18 at 22:23
  • Well if its not too much trouble.. Yeah that's the issue, there arent many test cases to begin with other than the ones given. Seem to be missing half of the 20 they blasted on this. Was hoping someone has done this before and could shed some light as to what might've happen – lws803 Feb 13 '18 at 23:04
  • See this SO question for a beautiful solution: https://stackoverflow.com/questions/16871113/how-to-match-dna-sequence-pattern – גלעד ברקן Feb 14 '18 at 00:32

0 Answers0