zoukankan      html  css  js  c++  java
  • Codeforces Round #265 (Div. 2) C 暴力+ 找规律+ 贪心

    C. No to Palindromes!
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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.

    题解转自:http://www.cnblogs.com/iwtwiioi/p/3961709.html

    题意:给你一个没有长度为2以上(包括2)的回文串的一个字符串,然后让你求比他大的所有排列且没有长度为2以上(包括2)的下一个最小的排列。

    剩下的1个半小时都在搞这题。。。。。。。。。。T_T

    神题。。

    我dfs写渣

    判重写渣

    然后滚粗。

    这里有个贪心。

    串的任意部分都不能是回文=串中任意连续3个字符互不相同

    那么如果从左往右找,那么只需要判断x-1和x-2与x不等即可

    其次我们要保证字典序最小,所以我们从右向左依次尝试修改,只要找到不符合回文条件的点,就修改它。这样保证了前边的最小。。

    而且能证明,后边一定能构造出一个不是回文的串。例如m=4,我可以构造abcabcabc。。。。。。。。。。。。(x-1和x-2与x不等)

    程序中有访问 char 数组的负下标,感觉太高大上了,我试了下,负下标的%d输出为0

    网上有人说原因是  :记住,下标只是个操作符。 x[n] 等价于 *(x + n)

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdlib>
     4 #include<cstdio>
     5 #include<algorithm>
     6 #include<cmath>
     7 #include<queue>
     8 #include<map>
     9 
    10 #define N 1005
    11 #define M 15
    12 #define mod 6
    13 #define mod2 100000000
    14 #define ll long long
    15 #define maxi(a,b) (a)>(b)? (a) : (b)
    16 #define mini(a,b) (a)<(b)? (a) : (b)
    17 
    18 using namespace std;
    19 
    20 int n,p;
    21 char s[N];
    22 
    23 char ok(int t)
    24 {
    25     char i;
    26     for(i=s[t]+1;i-'a'+1<=p;i++){
    27         if(s[t-1]!=i && s[t-2]!=i) return i;
    28     }
    29     return 0;
    30 }
    31 
    32 int solve()
    33 {
    34     int i;
    35     char t;
    36    // if(p==1) return 0;
    37    // if(n==1) return
    38     for(i=n-1;i>=0;i--){
    39         t=ok(i);
    40         if( t!=0 ){
    41             s[i]=t;
    42             break;
    43         }
    44     }
    45     if(i==-1) return 0;
    46 
    47     i++;
    48     for(;i<n;i++){
    49         s[i]='a'-1;
    50         t=ok(i);
    51         if( t!=0 ){
    52             s[i]=t;
    53         }
    54     }
    55     return 1;
    56 }
    57 
    58 int main()
    59 {
    60     //freopen("data.in","r",stdin);
    61     //scanf("%d",&T);
    62     //for(int cnt=1;cnt<=T;cnt++)
    63     //while(T--)
    64     while(scanf("%d%d",&n,&p)!=EOF)
    65     {
    66 
    67         scanf("%s",s);
    68        // if(p==1){
    69         //    printf("NO
    ");continue;
    70        // }
    71         if(solve()==0){
    72             printf("NO
    ");
    73         }
    74         else{
    75             printf("%s
    ",s);
    76         }
    77     }
    78 
    79     return 0;
    80 }
  • 相关阅读:
    计算机网络概述
    虚拟机网卡配置
    元类
    反射和内置方法
    面向对象的三大特性
    MYSQL中EXISTS的用法
    Guava中的常见集合操作用法
    集合操作交并补的三种Java实现
    P9 get和resize操作(Java 13)
    P8 Java 13中 HashMap的 put方法
  • 原文地址:https://www.cnblogs.com/njczy2010/p/3967834.html
Copyright © 2011-2022 走看看