zoukankan      html  css  js  c++  java
  • Codeforces Round #265 (Div. 2) C. No to Palindromes!

    Paul hates palindromes. He assumes that string s is tolerable if each its character is one of the first p letters of the English alphabet and s doesn't contain any palindrome contiguous substring of length 2 or more.

    Paul has found a tolerable string s of length n. Help him find the lexicographically next tolerable string of the same length or else state that such string does not exist.

    Input

    The first line contains two space-separated integers: n and p (1 ≤ n ≤ 1000; 1 ≤ p ≤ 26). The second line contains string s, consisting of n small English letters. It is guaranteed that the string is tolerable (according to the above definition).

    Output

    If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes).

    Sample test(s)
    Input
    3 3
    cba
    
    Output
    NO
    
    Input
    3 4
    cba
    
    Output
    cbd
    
    Input
    4 4
    abcd
    
    Output
    abda
    
    Note

    String s is lexicographically larger (or simply larger) than string t with the same length, if there is number i, such that s1 = t1, ..., si = ti, si + 1 > ti + 1.

    The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one.

    A palindrome is a string that reads the same forward or reversed.

    题意:求满足大于给定串的序列,并且两个序列都不包括长度大于等于2的回文子串

    思路:首先,由于不能有长度大于等于2的子串。所以当前位置是不能和它的前一个和前第二个一样的。

    由于给定串也是不包括回文子串的,所以为了能找个一个最小的满足条件的字符串。我们从右往前改。找到一个满足不与前两个一样的位置的话,那么从这个位置往右就是填写不构成回文的最小串了

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int maxn = 1010;
    
    int n, p;
    char str[maxn], tmp;
    
    int find(const int &x) {
    	if (x == -1) 
    		return 0;
    	for (int i = 1; ; i++) {
    		if (str[x] + i >= tmp) break;
    		if (x > 0 && str[x-1] == str[x] + i) continue;
    		if (x > 0 && str[x-2] == str[x] + i) continue;
    		str[x] += i;
    		return 1;
    	}
    
    	if (!find(x-1)) 
    		return 0;
    
    	str[x] = 'a';
    	for (int i = 0; ; i++) {
    		if (str[x] + i >= tmp) break;
    		if (x > 0 && str[x-1] == str[x] + i) continue;
    		if (x > 1 && str[x-2] == str[x] + i) continue;
    		str[x] += i;
    		return 1;
    	}
    	return 0;
    }
    
    int main() {
    	scanf("%d%d", &n, &p);	
    	tmp = 'a' + p;
    	scanf("%s", str);
    	if (find(n-1))
    		printf("%s
    ", str);
    	else printf("NO
    ");
    	return 0;
    }
    


  • 相关阅读:
    jstat命令行工具监控JVM内存和垃圾回收
    SkyWalking 日志监控
    SkyWalking 数据持久化
    问题记录: java 19000101 08:05:43 时间偏移bug
    springboot jest链接es
    redisRedisLockRegistry 分布式锁
    es 索引别名
    springboot elasticsearchresthighlevelclient 连接es
    缓存穿透、缓存击穿和缓存雪崩 概念
    Navicat for MySQL 导出中文乱码问题
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/6768683.html
Copyright © 2011-2022 走看看