逆天题目,写到最后真的想要放弃感觉自己真的好傻,打算写完暴力就收手看题解,没想到还过了,可能数据量比较少,
算法1
(暴力枚举) O(n2)
C++ 代码
include[HTML_REMOVED]
include[HTML_REMOVED]
include[HTML_REMOVED]
using namespace std;
const int N = 2e4 + 10;
struct Node{
char a, b;
};
vector[HTML_REMOVED] PII;
int ans = 0;
char an[N];
int n, F;
bool check(int i, int j, int x, int y){
if(i == 1){
for(int l = i + 1; l <= j; l ){
if(an[l] - 97 + 1 == x && an[l + 1] == an[l + 2] && an[l + 1] - 97 + 1 == y) return false;
}
}
else if(i == 2){
for(int l = i - 1; l <= j; l ){
if(an[l] - 97 + 1 == x && an[l + 1] == an[l + 2] && an[l + 1] - 97 + 1 == y) return false;
}
}
else {
for(int l = i - 2; l <= j; l ++){
if(an[l] - 97 + 1 == x && an[l + 1] == an[l + 2] && an[l + 1] - 97 + 1 == y) return false;
}
}
return true;
}
int main(){
cin>>n>>F;
for(int i = 1; i <= n; i ++) cin>>an[i];
int bn[30][30];
bool cn[30][30];
int a = 97;
memset(bn, 0, sizeof bn);
for(int i = 1; i + 2 <= n; i ++){
char ai = an[i], bi = an[i + 1], ci = an[i + 2];
if(ai != bi && bi == ci){
int x = ai - a + 1, y = bi - a + 1, z = ci - a + 1;
bn[x][y] ++;
}
}
for(int i = 1; i + 2 <= n; i ++){
char ai = an[i], bi = an[i + 1], ci = an[i + 2];
int x = ai - a + 1, y = bi - a + 1, z = ci - a + 1;
if(ci == bi && ai == ci){
for(int j = 1; j <= 26 ; j ++){
if(!cn[j][y] && j != x && check(i, i + 2, j, y)) bn[j][y] ++, cn[j][y] = true ;
}
}
else if(ci == bi && ai != ci){
for(int j = 1; j <= 26; j ++){
if(!cn[j][y] && j != x && j != y && check(i, i + 2, j, y)) bn[j][y] ++, cn[j][y] = true ;
}
}
else if(ai == bi && bi != ci ){
if(!cn[x][z] && check(i, i + 2, x, z)) bn[x][z] ++ , cn[x][z] = true;
}
else if(ai == ci && ai != bi){
if(!cn[x][y] && check(i, i + 2, x, y)) bn[x][y] ++, cn[x][y] = true ;
}
else{
if(!cn[x][y] && check(i, i + 2, x, y)) bn[x][y] ++, cn[x][y] = true ;
if(!cn[x][z] && check(i, i + 2, x, z)) bn[x][z] ++, cn[x][z] = true ;
}
}
for(int i = 1; i <= 26; i ++){
for(int j = 1; j <= 26; j ++){
if(bn[i][j] >= F) {
ans ++;
char ai = a + i - 1, bi =a + j - 1;
struct Node node = {ai, bi};
PII.push_back(node);
}
}
}
cout<<ans<<endl;
for(auto item : PII){
cout<<item.a<<item.b<<item.b<<endl;
}
}