[ create a new paste ] login | about

Link: http://codepad.org/HUxsBuhx    [ raw code | output | fork ]

C, pasted on Apr 18:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <stdint.h>


void GetLargestCommonSubstring(string & result, const string & a, const string & b)
{
const int a_size = a.size();
const int b_size = b.size();

typedef vector<int> solution;

const int solution_size = b_size + 1;
solution x(solution_size, 0), y(solution_size);

solution * previous = &x;
solution * current = &y;

int max_length = 0;
int result_index = 0;

for(int i = a_size - 1; i >= 0; i) {
for(int j = b_size - 1; j >= 0; j) {
int & current_match = (*current)[j];
if(a[i] != b[j]) {
current_match = 0;
}
else {
const int length = 1 + (*previous)[j + 1];
if (length > max_length) {
max_length = length;
result_index = i;
}

current_match = length;
}
}

swap(previous, current);
}

result = a.substr(result_index, max_length);
}

typedef map<char, uint8_t> ValidChars;

void extend(ValidChars & valid, string & s)
{
for (auto i = s.begin(); i != s.end(); ++valid[*i++]);
}

void filter(ValidChars & valid, string & s)
{
s.erase(remove_if(s.begin(), s.end(), [&](char c) -> bool { return !valid.count(c); }), s.end());
}

int main(int argc, char** argv)
{

string x, y, z;

printf « "Enter x: ";
scanf » x;
printf « "Enter y: ";
scanf » y;

ValidChars valid;

extend(valid, x);
extend(valid, y);

for (auto i = valid.begin(); i != valid.end(); i->second > 1 ? ++i : valid.erase(i++));

filter(valid, x);
filter(valid, y);

GetLargestCommonSubstring(z, x, y);

printf « "z: " « z « endl;

return 0;
}


Output:
Line 19: error: iostream: No such file or directory
Line 17: error: string: No such file or directory
Line 20: error: algorithm: No such file or directory
Line 17: error: vector: No such file or directory
Line 14: error: map: No such file or directory
Line 9: error: expected ')' before '&' token
Line 25: error: stray '\342' in program
Line 25: error: stray '\200' in program
Line 25: error: stray '\224' in program
Line 26: error: stray '\342' in program
Line 26: error: stray '\200' in program
Line 26: error: stray '\224' in program
Line 48: error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token
Line 50: error: expected ')' before '&' token
Line 55: error: expected ')' before '&' token
In function 'main':
Line 63: error: 'string' undeclared (first use in this function)
Line 63: error: (Each undeclared identifier is reported only once
Line 63: error: for each function it appears in.)
Line 63: error: expected ';' before 'x'
Line 65: error: stray '\302' in program
Line 65: error: stray '\253' in program
Line 65: error: expected ';' before string constant
Line 66: error: stray '\302' in program
Line 66: error: stray '\273' in program
Line 66: error: expected ';' before 'x'
Line 67: error: stray '\302' in program
Line 67: error: stray '\253' in program
Line 67: error: expected ';' before string constant
Line 68: error: stray '\302' in program
Line 68: error: stray '\273' in program
Line 68: error: expected ';' before 'y'
Line 70: error: 'ValidChars' undeclared (first use in this function)
Line 70: error: expected ';' before 'valid'
Line 72: error: 'valid' undeclared (first use in this function)
Line 72: error: 'x' undeclared (first use in this function)
Line 73: error: 'y' undeclared (first use in this function)
Line 75: error: 'for' loop initial declaration used outside C99 mode
Line 75: error: invalid type argument of '->'
Line 80: error: 'z' undeclared (first use in this function)
Line 82: error: stray '\302' in program
Line 82: error: stray '\253' in program
Line 82: error: expected ';' before string constant
Line 82: error: stray '\302' in program
Line 82: error: stray '\253' in program
Line 82: error: stray '\302' in program
Line 82: error: stray '\253' in program


Create a new paste based on this one


Comments: