-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotepad.cpp
57 lines (51 loc) · 1.7 KB
/
Notepad.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
Problem statement:
You want to type the string s, consisting of n lowercase Latin letters, using your favorite text editor Notepad#.
Notepad# supports two kinds of operations:
1 - append any letter to the end of the string;
2 - copy a continuous substring of an already typed string and paste this substring to the end of the string.
Can you type string s in strictly less than n operations?
Input
The first line contains a single integer t — the number of testcases.
The first line of each testcase contains a single integer n — the length of the string s.
The second line contains a string s, consisting of n lowercase Latin letters.
The sum of n doesn't exceed 2⋅105 over all testcases.
Output
For each testcase, print "YES" if you can type string s in strictly less than n operations. Otherwise, print "NO".
------------------------------------------------------YOU CAN FIND THIS PROBLEM ON THIS LINK :: https://codeforces.com/problemset/problem/1766/B
*/
#include <iostream>
#include <map>
#include <set>
using namespace std;
int main()
{
int T;
cin >> T;
while (T--) {
int n,flag=0;
string s;
map<char, set<char>> m;
cin >> n >> s;
if (n == 3) {
cout << "NO\n";
}
else {
m[s[0]].insert(s[1]);
for (int i = 3; i < n; i++) {
if (m[s[i - 1]].find(s[i]) != m[s[i-1]].end()) {
cout << "YES\n";
flag = 1;
break;
}
else {
m[s[i-2]].insert(s[i-1]);
}
}
if (flag == 0) {
cout << "NO\n";
}
}
}
return 0;
}