题解 P6739 【[BalticOI 2014 Day1] Three Friends】
LongDouble · · 题解
看了下题解,全是哈希。
这里写一个更简单的做法。
思路
由题意可知
由题意可知
因为只插入一个字符,所以如果存在
所以可以用 substr 函数分别截取前
代码
#include <bits/stdc++.h>
using namespace std;
int n, m, a1, a2;
string u, s1, s2;
int main()
{
scanf("%d", &n);
if (n % 2 == 0)
{
printf("NOT POSSIBLE\n");
return 0;
}
cin >> u;
m = n / 2;
s1 = u.substr(0, m); //匹配检查前M个字符
int j = 0;
for (int i = m; i < n && j < m; i++)
if (u[i] == s1[j]) j++;
if (j == m) a1 = 1;
s2 = u.substr(n - m, m); //匹配检查后M个字符
j = 0;
for (int i = 0; i < n - m && j < m; i++)
if (u[i] == s2[j]) j++;
if (j == m) a2 = 1;
if (!a1 && !a2) printf("NOT POSSIBLE\n");
else if (a1 && a2 && s1 != s2) printf("NOT UNIQUE\n");
else if (a1) cout << s1 << endl;
else cout << s2 << endl;
return 0;
}