zoukankan      html  css  js  c++  java
  • POJ 3096 Surprising Strings

    Surprising Strings

    Time Limit: 1000ms
    Memory Limit: 65536KB
    This problem will be judged on PKU. Original ID: 3096
    64-bit integer IO format: %lld      Java class name: Main
     
     

    The <dfn>D-pairs of a string of letters are the ordered pairs of letters that are distance D from each other. A string is <dfn>D-unique if all of its D-pairs are different. A string is <dfn>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

     
    解题:直接暴力了,利用map!其实set也可以。题意就是找出距离为k的两个字符组成的串。在k的距离下,有两个或者多个串一样。那么不surprising了。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <map>
    13 #include <stack>
    14 #define LL long long
    15 #define pii pair<int,int>
    16 #define INF 0x3f3f3f3f
    17 using namespace std;
    18 map<string,int>mp;
    19 char str[100],tmp[5];
    20 int main() {
    21     while(~scanf("%s",str)){
    22         if(str[0] == '*') break;
    23         int len = strlen(str);
    24         bool flag = true;
    25         for(int k = 1; k < len; k++){
    26             mp.clear();
    27             for(int j = 0; j+k < len; j++){
    28                 tmp[0] = str[j];
    29                 tmp[1] = str[j+k];
    30                 tmp[2] = '';
    31                 if(mp[tmp]) {flag = false;break;}
    32                 mp[tmp]++;
    33             }
    34             if(!flag) break;
    35         }
    36         if(flag) printf("%s is surprising.
    ",str);
    37         else printf("%s is NOT surprising.
    ",str);
    38     }
    39     return 0;
    40 }
    View Code
  • 相关阅读:
    C++中头文件包含的问题
    linux环境变量
    win32进程和线程
    断言
    win32中的常用类型转换
    可变形参
    #define
    CString与char*互相转化
    extern
    手机CPU和GPU厂商
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3934358.html
Copyright © 2011-2022 走看看