Surprising Strings
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5783   Accepted: 3792

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

解题思路:

推断一个字符串是不是surpring。

条件:该字符串的全部D-pairs,D是字符串中两个字母的距离, 假设全部的D-pairs都不同,那么该字符串是D-unique。

假设对全部的距离D,都满足D-unique,那么字符串就是surpring.

比方ZGBG

0-pairs :  ZG  GB BG  不同样,是0-unique

1-pairs :  ZB  GG        不同样,是1-unique

2-pairs:   ZG               不同样。是2-unique

综上。ZGBG是surpring

每个D-pairs进行推断。假设在推断一个D-pairs过程中有同样的,那么该字符串肯定不是surpring。

用map<string,bool> 来推断是否字符串已经出现过。要注意其声明的位置,要在每一层D循环里面声明。

代码:

#include <iostream>
#include <stdio.h>
#include <map>
#include <string.h>
using namespace std;
char str[80]; int main()
{
while(scanf("%s",str)!=EOF&&str[0]!='*')
{
int len=strlen(str);
if(len<=2)
{
cout<<str<<" is surprising."<<endl;
continue;
}
bool ok=1;//是surpring
for(int d=0;d<=len-2;d++)//距离d
{
bool dtap=1;//是D-unique
map<string,bool>mp;//注意其声明的位置
for(int s=0;s<=len-2&&s+d+1<len;s++)
{
string temp="";
temp+=str[s];
temp+=str[s+d+1];
if(mp[temp])//该字符串出现过
{
dtap=0;
break;
}
else
mp[temp]=1;
}
if(!dtap)
{
ok=0;
break;
}
}
if(ok)
cout<<str<<" is surprising."<<endl;
else
cout<<str<<" is NOT surprising."<<endl;
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

[ACM] POJ 3096 Surprising Strings (map使用)的更多相关文章

  1. POJ 3096 Surprising Strings

    Surprising Strings Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5081   Accepted: 333 ...

  2. 【字符串题目】poj 3096 Surprising Strings

    Surprising Strings Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6193   Accepted: 403 ...

  3. POJ 3096 Surprising Strings(STL map string set vector)

    题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...

  4. POJ 3096:Surprising Strings

    Surprising Strings Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6258   Accepted: 407 ...

  5. [POJ3096]Surprising Strings

    [POJ3096]Surprising Strings 试题描述 The D-pairs of a string of letters are the ordered pairs of letters ...

  6. C - Surprising Strings

                                   C - Surprising Strings 题意:输入一段字符串,假设在同一距离下有两个字符串同样输出Not surprising ,否 ...

  7. HDOJ 2736 Surprising Strings

    Surprising Strings Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. HDU 2736 Surprising Strings

                                    Surprising Strings Time Limit:1000MS     Memory Limit:65536KB     64 ...

  9. poj 2406 Power Strings 后缀数组解法

    连续重复子串问题 poj 2406 Power Strings http://poj.org/problem?id=2406 问一个串能否写成a^n次方这种形式. 虽然这题用kmp做比较合适,但是我们 ...

随机推荐

  1. 闪回drop恢复表后sql运行计划异常

    -----正常运行计划 set autotrace traceonly set linesize 1000 select /*+index(t idx_object_id)*/ * from t wh ...

  2. keil快捷键

  3. iOS开发Quartz2D之八:图形上下文状态栈

    #import "DrawView.h" @implementation DrawView - (void)drawRect:(CGRect)rect { // Drawing c ...

  4. csdn博客栏目加入微博关注

    大家首先切换到:博客专栏,然后点击"加入专栏".然后直接复制下述代码就能够了: <a href="http://weibo.com/u/3247569660/hom ...

  5. Java Queue的使用

    Queue的成员函数        add        增加一个元索                     如果队列已满,则抛出一个IIIegaISlabEepeplian异常       rem ...

  6. [Angular] Create custom validators for formControl and formGroup

    Creating custom validators is easy, just create a class inject AbstractControl. Here is the form we ...

  7. Android中的动画详解系列【1】——逐帧动画

    逐帧动画其实很简单,下面我们来看一个例子: <?xml version="1.0" encoding="utf-8"?> <animation ...

  8. [Jenkins] Creating Application builds

    After installing the jenkins, we start creating new job. 1. Give job names (your project name): 2. G ...

  9. js进阶 11-7 jquery如何获取和改变元素的位置

    js进阶 11-7  jquery如何获取和改变元素的位置 一.总结 一句话总结:jquery中匿名函数中的index参数是什么意思.jquery对象多集合,故index为所选元素的下标. 1.jqu ...

  10. 关于ulimit -a中需要修改的两个值

    以root用户运行 ulimit -a 命令,其中有两个参数分别为: open files和max user processes   修改方法:  vi /etc/security/limits.co ...