zoukankan      html  css  js  c++  java
  • poj3096

    C++的标准模版库的应用

    Surprising Strings
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 6625   Accepted: 4309

    Description

    The D-pairs of a string of letters are the ordered pairs of letters that are distance D from each other. A string is D-unique if all of its D-pairs are different. A string is surprising if it is D-unique for every possible distance D.

    Consider the string ZGBG. Its 0-pairs are ZG, GB, and BG. Since these three pairs are all different, ZGBG is 0-unique. Similarly, the 1-pairs of ZGBG are ZB and GG, and since these two pairs are different, ZGBG is 1-unique. Finally, the only 2-pair of ZGBG is ZG, so ZGBG is 2-unique. Thus ZGBG is surprising. (Note that the fact that ZG is both a 0-pair and a 2-pair of ZGBG is irrelevant, because 0 and 2 are different distances.)

    Acknowledgement: This problem is inspired by the "Puzzling Adventures" column in the December 2003 issue of Scientific American.

    Input

    The input consists of one or more nonempty strings of at most 79 uppercase letters, each string on a line by itself, followed by a line containing only an asterisk that signals the end of the input.

    Output

    For each string of letters, output whether or not it is surprising using the exact output format shown below.

    Sample Input

    ZGBG
    X
    EE
    AAB
    AABA
    AABB
    BCBABCC
    *

    Sample Output

    ZGBG is surprising.
    X is surprising.
    EE is surprising.
    AAB is surprising.
    AABA is surprising.
    AABB is NOT surprising.
    BCBABCC is NOT surprising.

    Source

    题解:
    给一个字符串,要求,对于这个字符串空隔为k取字符对(k=0,1,2,3,4...)要求在相同的空隔取对过程汇总,整个字符串中没有一个相同字符对如:
    ZGBZ:
    间隔为0的字符对有: ZG、GB、BZ,三个均不相同
    间隔为1的字符对有: ZG、 GZ,均不相同
    间隔为2的字符对有: ZZ 仅有一个,不必比较。
    这种字符串定义为"surprising".
    之后按照格式输出。
     
    看到这个题目,一开始一点思路都没有,不过从样例可知,单个字符和两个字符的情况下,不用比较,直接定义为“surprising”。如果用纯模拟,就是分别去不同的隔断,然后,分别取出字符对来进行比较,这种是纯暴力的方法;之后想到了,或许可以按照某一种方式来对字符串进行错位后的字符进行比较,就可以每一次比较到从头到尾,每一个字符在多种间隔的情况下是否存在相同,看如下解释:
    第一次错位,错位量为1:
     
     
    上下比较,仅有一对“C”相同;
    第二次错位,错位量为2:
     
     
    发现有两列相同,两个BB,则我们可以知道有:
    两个字符对是相等的BB、BB。
     
    即是说,我们每次循环一次,从头到尾分别取到间隔从0到m的字符对,只要有两次相同,我们就可以认为他是一个字符对相同,然后退出循环,输出。
    直到所有的循环结束,还没有找到相同的字符对,我们才认为这个是“surprising”。
    AC代码
    #include<cstdio>
    #include<cstring>
    char str[100];
    int main(){
        int i,j,k;
        int count;
        int len;
        while(scanf("%s",str)==1&&strcmp(str,"*")!=0){
            len=strlen(str);
            if(len<=2){
                printf("%s is surprising.
    ", str);
                continue;
            }
            for(i=0,count=0;i<len&&count!=2; i++){
                count=0;
                for(j=i+1,k=0;j<len;j++,k++){
                    if(str[j]==str[k]) count++;
                    if(count==2) break;
                }
            }
            if(count==2)
                printf("%s is NOT surprising.
    ", str);
            else
                printf("%s is surprising.
    ", str);
        }
        return 0;
    }
  • 相关阅读:
    django路由——关于路由最后斜杠的问题
    django-admin 汉化
    动态绑定属性、方法 以及 __slots__
    type()、获取对象信息
    String,StringBuffer,StringBuilder三种的性能差异
    线程创建的三种方式的比较(继承Thread,Runnable接口,Callable接口)
    单例设计模式
    用Dockerfile来build原理和过程
    etcd和redis
    win10部署docker和mongo
  • 原文地址:https://www.cnblogs.com/shenben/p/5601098.html
Copyright © 2011-2022 走看看