Introduction to the classic algorithm contest (second edition) 3-11 kickdown device UVA-1588

This title like a long time ah! ! ! Also a variety of small details wrong, I too dishes to try harder

This time take solving ideas to write about, or estimate a few days to forget

Problem-solving ideas:

Long fixed, mobile short strips to match long, there are three cases

First, the short bar to move within the long range match

 

 

The second, short bar to move to the left to match the strip

 

Third, short strips on the right long, in the case of the left Similarly, the right moves to a position equal to 0 and the stop 5 matched

 

Implementation code

#include <stdio.h> 
#include < String .h> 
#include <stdlib.h>
 #define MAXN 100 int match ( int B1 [MAXN], int B2 [MAXN], int LEN1, int LEN2) 
    { //     int LEN1 = length1, LEN2 = length2; int min = LEN1 + LEN2, in Flag = 0 ;
     for ( int K = 0 ; K <= LEN2-LEN1; K ++) // short train is within the long string     {
         int T = 0 ;
         for (; T <LEN1; T ++) // left to right, the movement of matching short tandem

    

    

        {
             // the printf ( "B1 [D%]% = D B2 [D%] D =% \ n-", T, B1 [T], T + K, B2 [T + K]); 
            IF (B1 [T ] + B2 [T + K]> . 3 ) 
            { 
                BREAK ; 
            } 
        } 
        IF (LEN1 == T) = {In Flag . 1 ; BREAK ;} 
                        
    } 
    IF (In Flag == . 1 ) = min {LEN2; return (min);}     
    
    the else 
{             
    int R & lt = . 1 ;    
     for (; R & lt <LEN1; R & lt ++) // short string on the left, moving to the left where 
    {
         int S = 0 ;
         for(; S <LEN1-R & lt; S ++) // individually matched 
        {
             IF (B1 [R & lt S +] + B2 [S]> . 3 ) { BREAK ;}         
        } 
        IF (S-R & lt == LEN1) = {In Flag . 1 ; BREAK ;}         
    } 
    IF (in Flag == . 1 ) = min {LEN2 + R & lt;} 
        
    int n-= . 1 ; in Flag = 0 ;
     for (; n-<LEN1; n-++) // short string on the right, rightward movement of 
    {
     int len1- = m . 1 ;
     for (; m> = n-; M--) // individually matched 
    {
         //printf("b1[%d]=%d b2[%d]=%d\n",m-n,b1[m-n],m-len1+len2,b2[m]);
        if(b1[m-n]+b2[m-len1+len2]>3){break;}
        
        }
        if(m<n){flag=1;break;}    
    } 

     if(flag==1){min=len2+n>min?min:len2+n ;return min;}
     else{return min;}
                     
}    
    }    
    
int main()
{
    char a1[maxn],a2[maxn];
    int c1[maxn]={0},c2[maxn]={0};
    gets(a1);
    gets(a2);
    int length1=strlen(a1);
    int length2=strlen(a2);
    
    for(int i=0;i<length1;i++)
    {
        c1[i]=a1[i]-48;//'0'的ASCII码=48
    }    

    for(int j =0;j<length2;j++)
    {
        c2[j]=a2[j]-48;
    }
    int num=length1<=length2?match(c1,c2,length1,length2):match(c2,c1,length2,length1);
    printf("Min_length=%d",num);
}

https://vjudge.net/problem/UVA-1588

 

Guess you like

Origin www.cnblogs.com/lytuser/p/11734433.html