Algorithm


 problem Link :  https://onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1963 

Spotting patterns in seemingly random strings is a problem with many applications. E.g. in our efforts to understand the genome we investigate the structure of DNA strings. In data compression we are interested in finding repetitions, so the data can be represented more efficiently with pointers. Another plausible example arises from the area of artificial intelligence, as how to interpret information given to you in a language you do not know. The natural thing to do in order to decode the information message would be to look for recurrences in it. So if the SETI project (the Search for Extra Terrestrial Intelligence) ever get a signal in the H21-spectra, we need to know how to decompose it. One way of capturing the redundancy of a string is to find its factoring. If two or more identical substrings A follow each other in a string S, we can represent this part of S as the substring A, embraced by parentheses, raised to the power of the number of its recurrences. E.g. the string DOODOO can be factored as (DOO)2 , but also as (D(O)2 ) 2 . Naturally, the latter factoring is considered better since it cannot be factored any further. We say that a factoring is irreducible if it does not contain any consecutive repetition of a substring. A string may have several irreducible factorings, as seen by the example string POPPOP. It can be factored as (POP)2 , as well as PO(P)2OP. The first factoring has a shorter representation and motivates the following definition. The weight of a factoring, equals the number of characters in it, excluding the parentheses and the exponents. Thus the weight of (POP)2 is 3, whereas PO(P)2OP has weight 5. A maximal factoring is a factoring with the smallest possible weight. It should be clear that a maximal factoring is always an irreducible one, but there may still be several maximal factorings. E.g. the string ABABA has two maximal factorings (AB)2A and A(BA)2 . Input The input consists of several rows. The rows each hold one string of at least one, but less than 80 characters from the capital alphabet A-Z. The input is terminated by a row containing the character ‘*’ only. There will be no white space characters in the input. Output For each string in the input, output one line containing the weight of a maximal factoring of the string.

Sample Input PRATTATTATTIC GGGGGGGGG PRIME BABBABABBABBA ARPARPARPARPAR *

Sample Output 6 1 5 6 5

Code Examples

#1 Code Example with C Programming

Code - C Programming

#include <bits/stdc++.h>

using namespace std;

int main()
{
    string in;
    while(getline(cin,in),in!="*"){
        vector < vector<int>> dp(in.length(),vector<int>(in.length(),1e6));
        for(int i=0;i < in.length();i++)
            for(int j=i;j < in.length();j++)
                dp[i][j] = j-i+1;

        for(int len=1;len<in.size();len++){
            for(int start=0;start+len<in.size();start++){
                int high=start+len;
                int full_len = high-start+1;
                for(int mid=start;mid<high;mid++)
                    dp[start][high] = min(dp[start][high],dp[start][mid]+dp[mid+1][high]);

                for(int sub_len=1;sub_len < =full_len/2;sub_len++){
                    if(full_len%sub_len == 0){
                        // check if start to high have a cycle repetition of sub_len
                        // that is (sub_str)^k
                        string pattern = in.substr(start,sub_len);
                        bool match = true;
                        for(int j=start+sub_len;j<=high && match;j+=sub_len){
                            string haystack = in.substr(j,sub_len);
                            if(pattern != haystack) match = false;
                        }
                        if(match){
                            dp[start][high] = min(dp[start][high],dp[start][start+sub_len-1]); // count only first cycle
                        }
                    }
                }
            }
        }
        cout << dp[0][in.length(>-1] << endl;
    }
}
 
Copy The Code & Try With Live Editor

Input

x
+
cmd
PRATTATTATTIC
GGGGGGGGG
PRIME
BABBABABBABBA
ARPARPARPARPAR
*

Output

x
+
cmd
6
1
5
6
5
Advertisements

Demonstration


UVA Online Judge solution - 11022-String Factoring - UVA Online Judge solution in C,C++,java

Previous
UVA Online Judge solution -11003-Boxes - UVA Online Judge solution in C,C++,java
Next
UVA Online Judge solution - 11026-A Grouping Problem - UVA Online Judge solution in C,C++,java