POJ-3096

Surprising Strings

Time Limit: 1000MS memory Limit: 65536K

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.


題目大意:
給定一個字符串,如果這個字符串按照間隔 d 取字符對的過程中,對於相同的d沒有相同字符對是相同的。 那麼這個字符串就是 d-unique的。
而如果這個字符串對於每一個di都是 di-unqiue的話,這個字符串就滿足surprising的。
現在給你幾個字符串,請你依次判斷他們是不是surprising的。
解題思路:
首先如果字符串長度小於等於2的話,顯然是滿足的。只有長度超過了2,例如”AAA”,它就是不滿足1-unique的,所以不是surprising的。但是它是2-unqiue的,因爲它只有一個2-pairs的字符對“AA”。從中,我們也就知道,對於一個長度爲n(n>2)的字符串,如果驗證了3,4,… ,n-1 unique都滿足的話就認爲它是surprising的了。
小技巧
對於每一個字符對,可以將它記錄爲一個數。由於題目中確認輸入中只包含大寫字母,那麼就可以把這個字符對看成一個兩位數。
x = int(arr[j] - 'A') * 26 + int(arr[j + i] - 'A');
另外注意只要中間發現一個不滿足,是可以跳出多層循環的。


源代碼

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;

char  arr[80];
int num;
int main() {
    int i, j,k,x,y;
    bool flag;
    scanf("%s", arr);
    while (arr[0] != '*') {
        flag = true;
        num = strlen(arr);
        if (num >2) {
            for (i = 1; i < num && flag; i++) {
                for (j = 0; j < num - i-1 && flag; j++) {
                    x = int(arr[j] - 'A') * 26 + int(arr[j + i] - 'A');
                    for (k = 1; k < num - j - i; k++) {
                        y = int(arr[j + k] - 'A') * 26 + int(arr[j + k + i] - 'A');
                        if (x == y) {
                            flag = false;
                            break;
                        }
                    }

                }
            }
        }
        if (flag) {
            printf("%s is surprising.\n",arr);
        }
        else {
            printf("%s is NOT surprising.\n", arr);
        }
        scanf("%s", arr);
    }

    return 0;
}
發佈了30 篇原創文章 · 獲贊 14 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章