File virus infection reads output

Detecting a series of n individual viral DNA sequence and there a match, wherein the virus is a ring that is not simply a predetermined length of string

The following problem-solving ideas talk about it

First, just-the first to write a big frame that is the main function

Method: two strings is defined by The freopen (input file name, "r read", stdin) freopen (the output file, "w write", stdout), reads the number of inspectors, read from the beginning of a cycle with a into the base string and the string pattern, the comparison returns a result, the output according to the result

int main () 
{ 
    String S, T;
     int n-; 
    The freopen ( " input data .txt " , " R & lt " , stdin); 
    The freopen ( " output .txt " , " W " , stdout); 
    CIN >> n-;   // number used as the file number N 
    for ( int I = . 1 ; I <= n-; ++ I) 
    { 
        CIN >> >> T S;   // COUT << T << S 
        COUT << T << "  " << S <<" ";
        IF (virus_detection (S, T) == 1 )   // returns the result of an infection, uninfected is 0 
        COUT << " Yes " << endl;
         the else  
        COUT << " NO " ; 
    } 
    return  0 ; 
        // met output of only one because the original is placed within the loop return 0 leads to the end of the first cycle is the entire cycle ended 
 }

Continue to refine the interface virus-detection function

Methods: t + = t to the original pattern string lengthened twice the original string and each pattern string length, taken as a new pattern string compared with the parent sequence, with the results acquired flag (if there is a partial match), according to the result returns whether infection

int virus_detection ( String S, String T) 
{   // detect infection 
    int NUM = t.length ();
     int In Flag;   // define a recording mode flag to the parent string matching results 
    String TEMP;   // new strings do comparison of pattern strings for each 
    t = t +;   // virus string is doubled 
    for ( int i = 0 ; i <NUM; i ++ ) 
    { 
           temp.assign (t, i, NUM);   
        // the content from the position t i num characters from the original string as a new content of the original string is assigned 
           in Flag = index_bf (S, TEMP, . 1 );   // returns the result of the comparison, match the positions do not match -1 
           IF ! (in Flag = -1 )   // ! (I.e., does not match the result return uninfected -1), i.e., infection 
           return  1 ; 
    } 
    return  0 ;   // uninfected return 0 
}

assign a function usage of string class:

a. string& assign ( const string& str );

Str string replaces the original content
b string & assign (const string & str, size_t pos, size_t n).;

The contents of the original string str is assigned to the starting position pos n characters as a new content of the original string
d string & assign (const char * s).;

The character string or an array of strings as a new content to replace the original
e string & assign (size_t n, char c).;

Replace the original string n characters

The final step in the refinement index-bf function

Methods: This is a simple algorithm bf on the books, no two strings to the end of each character on each comparison, if the two strings are equal, moved back one position, otherwise i returned i-j + 1 (in fact, i is the original initial next), j back to 0 subscript position, pay attention to this question is not here, but from the start from the coordinate 0 1

int index_bf(string s,string t,int pos)
{
    int i=pos-1;
    int j=0;
    while(i<s.length()&&j<t.length())
    {
        if(s[i]==t[j])
        {
            ++i;   ++j;
        }
        else
        {
            i=i-j+1;   //i=i-j-1
            j=0;
        }
    }
    if(j==t.length())
    returnit.length ();   // returned matches the position of 
    the else  
    return - . 1 ;   // do not match -1 
 }

freopen function

Redirect the output, the output is only possible to the original character in the console, you specify the path to the output file. (Similar input is read from the specified file, instead of reading input in the console.) Redirection function can be turned on at any time to close.

Function name: freopen 

Standard Disclaimer: FILE * freopen (const char * path, const char * mode, FILE * stream); 

Where file: <stdio.h>

path: file name, file name for custom storage input and output. 

mode: file should be opened. And a mode fopen (e.g., read only r-, w- writing) the same. 

stream: a file, typically use standard streaming file. 

Return Value: success, a pointer to the specified file path is returned; failure returns NULL.

Function: redirection, the orientation of predefined criteria stream file specified by the file path. Standard stream file specifically refers to stdin, stdout and stderr. Wherein stdin is the standard input stream, the default keyboard; stdout is the standard output stream, default screen; stderr is the standard error, the screen is generally set as the default.

E.g:

#include <stdio.h> 
#include <the iostream>
 int main () 
{ 
    int A, B; 
    The freopen ( " D: \\ in.txt " , " R & lt " , stdin); // input redirection, the input data in.txt read from the file in the root directory D 
    the freopen ( " D: \\ out.txt " , " W " , stdout); // output redirection, the output data stored in the root directory of the D out.txt file 
    the while (CIN >> A >> B) 
    COUT << A + B << endl; // Note the use endl 
    fclose (stdin); // Close input redirection 
    fclose (stdout);//Close to redirect the output 
    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/jingjing1234/p/10720808.html