fragment-header
fragment-markdown
홈
로그인
로그아웃
내 블로그
설정
로그인
백준 25995 (C++) Dividing DNA
최초 업로드: 2025-09-24 00:38:47
최근 수정 시간: 2025-09-24 00:38:47
게시자: rlatjwls3333
카테고리: 백준
조회수: 4
# [Gold II] Dividing DNA [문제 링크](https://www.acmicpc.net/problem/25995) ## 문제 설명 <p>At the Bacteria And Protein Centre, you own a large collection of DNA. In fact, new strands of DNA come in all the time. To organise the vast amount of data, you identify each piece by its unique substrings: substrings that do not already occur in the database.</p> <p>Your database can quickly determine whether a given piece of DNA occurs as a substring in the database or not. Naturally, if a certain DNA string is found in the database, it also contains all its substrings.</p> <p>You now want to determine the <em>uniqueness</em> of a given piece of DNA: the maximal number of disjoint substrings it contains that are absent from the database.</p> <p>You are given the length $n$ of the query string $q_1\dots q_n$, and you can repeatedly ask the database whether it contains the substring $q_i\dots q_{j-1}$.</p> <p>As an example, consider the first sample interaction. In this case, the database contains strings "<code>TGC</code>" and "<code>CT</code>", and the query string is "<code>CTGCAA</code>". It has uniqueness $3$, because it can be split into the new substrings "<code>CTGC</code>", "<code>A</code>", and "<code>A</code>". The new substring "<code>CTGC</code>" cannot be split up further: for example, the subdivision "<code>CT</code>" and "<code>GC</code>" is not allowed, because both substrings occur (possibly as substrings) in the database. Note that the actual characters in the string are not used in the interaction.</p> <p>You may use at most $2n$ queries to the database.</p> ## 인터랙션 <p>This is an interactive problem. Your submission will be run against an <em>interactor</em>, which reads from the standard output of your submission and writes to the standard input of your submission. This interaction needs to follow a specific protocol:</p> <p>The interactor first sends one line with an integer $n$ ($1\leq n\leq 10\,000$), the length of the DNA piece for which you need to compute the uniqueness.</p> <p>Then, your program should make at most $2n$ queries. Each query is made by printing one line of the form "<code>?</code> $i$ $j$" ($0\leq i < j \leq n$), indicating that you want to query whether the substring starting at position $i$ and ending at position $j-1$ occurs in the database. The interactor will respond with either "<code>present</code>" or "<code>absent</code>", indicating whether the substring was found in the database.</p> <p>When you have determined the uniqueness $x$ of the piece of DNA, print one line of the form "<code>!</code> $x$", after which the interaction will stop. Printing the answer does not count as a query.</p> <p>Make sure you flush the buffer after each write.</p> <p>A testing tool is provided to help you develop your solution.</p> <p>Using more than $2n$ queries will result in a wrong answer.</p> ## 풀이 가장 작은 absent인 문자열에 present인 문자열을 붙인다 하더라도 absent입니다. 따라서 present인 문자열은 전부 바로 옆 absent에 붙여 개수를 세지 않고, absent인 문자열의 수만 세면 됩니다. ``` c++ #include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int cnt=0, left=0; for(int right=1;right<=n;right++) { cout << "? " << left << ' ' << right << '\n' << flush; string input; cin >> input; if(input[0]=='a') { left=right; cnt++; } } cout << "! " << cnt << '\n' << flush; } ```