Loj537 solution to a problem [] "LibreOJ NOIP Round # 1" DNA sequence

Title Description

\ (NOIP \) before rematch \ (HSD \) Sang conducted a study and found that a section on human chromosome \ (the DNA \) sequence of successive \ (K \) bases and a nucleotide sequence consisting of do question the \ (AC \) rate related! So he wanted to look at this relationship.

Now given period \ (the DNA \) sequence, to help him determine this \ (the DNA \) sequence all consecutive \ (K \) the nucleotide sequence of bases is formed, the number of times of occurrence of one kind occurs most .

Input Format

Two rows, the first row period \ (the DNA \) sequence, to ensure \ (the DNA \) sequence method, i.e. contains only \ (A, G, C, T \) four bases;
second line a positive integer \ ( k \) , meaning the same title description.

Output Format

Line, a positive integer, subject to the requirements described in the answer.

Sample

Sample input 1

AAAAA
1

Sample output 1

5

Sample Explanation 1

For this \ (the DNA \) sequence, successive \ (1 \) a nucleotide sequence consisting of only the bases

A , a total appears \ (5 \) times, so the answer is \ (5 \) .

Sample input 2

ACTCACTC
4

Sample output 2

2

Sample interpretation 2

For this \ (the DNA \) sequence, a continuous \ (4 \) a nucleotide sequence consisting of bases:

\ (ACTC, CTCA, TCAC \ ) and \ (CACT \) . Where \ (ACTC \) appears \ (2 \) times, the rest were appeared \ (1 \) times, so most frequently appears as \ (2 \) , is the answer.

Data range and tips

Note \ (the DNA \) sequences of length \ (n-\) .

This question altogether \ (10 \) sets of data, only output standard output before they can receive the same score for the test points.

Each given data range and satisfies the following properties: The

Properties: given \ (the DNA \) the nucleotide sequence of each base are the same.
For all data are guaranteed \ (k \ leq n \)

answer

This problem is easy to think of using \ (STL \) is \ (map \) to solve.

However, after my attempt, \ (the Map \) in this problem can only get \ (80 \) score, the question people get stuck.

However, \ (unordered \) _ \ (the Map \) in this question and will not be card.

So with \ (unordered \) _ \ (the Map \) can be friends \ (QwQ \) .

(Provided that the evaluation open \ (C ++ 11 \) )

Code

#pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <string>
#include <unordered_map>

using namespace std;

inline int gi()
{
    int f = 1, x = 0; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar();}
    while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar();}
    return f * x;
}

string s;
int len, k, n, m, ans, js, p[9];
unordered_map <string, int> ap;

int main()
{
    cin >> s;
    k = gi();
    len = s.size();
    bool fl = true;
    for (int i = 1; i < len; i++)
    {
        if (s[i] != s[i - 1]) fl = false; 
    }
    if (fl)
    {
        if (k == 1)
        {
            printf("%d\n", len);
            return 0;
        }
        else
        {
            printf("%d\n", len - k + 1);
            return 0;
        }
    }
    else if (k == 1)
    {
        for (int i = 0; i < len; i++)
        {
            if (s[i] == 'A') ++p[1];
            else if (s[i] == 'G') ++p[2];
            else if (s[i] == 'C') ++p[3];
            else ++p[4];
        }
        printf("%d\n", max(p[1], max(p[2], max(p[3], p[4]))));
        return 0;
    }
    else
    {
        for (int i = 0; i < s.size() - k + 1; i++) ++ap[s.substr(i, k)];
        for (auto it : ap) ans = max(ans, it.second);
        printf("%d\n", ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/xsl19/p/11220279.html