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. Setup iOS Development Environment.

    Setup iOS Development Environment Install XCode and check-out source code from SVN XCode Please find ...

  2. java I/O库的设计模式

    在java语言 I/O库的设计中,使用了两个结构模式,即装饰模式和适配器模式.      在任何一种计算机语言中,输入/输出都是一个很重要的部分.与一般的计算机语言相比,java将输入/输出的功能和使 ...

  3. stm32的ADC规则组通道采样顺序设置

    先看一下固件库手册 再看一下手册上的例子:  有两个通道,,并且顺序如下

  4. 利用a标签导出csv文件

    原文 简书原文:https://www.jianshu.com/p/a8687610cda3 大纲 1.需求分析 2.通过a标签实现文件导出 3.实现方式 1.需求分析 导出文件,使用最多的方式还是服 ...

  5. ZOJ 2514 Generate Passwords 水

    啦啦啦,水一发准备去复习功课~ ------------------------------------------水一发的分割线----------------------------------- ...

  6. IntelliJ IDEA设置鼠标悬浮提示

    测试代码; public interface MyInterface { /** * 我是接口方法的注释 * @param num1 我是接口方法入参的注释 * @return 我是接口方法返回值的注 ...

  7. [GraphQL] Write a GraphQL Mutation

    In order to change the data that we can query for in a GraphQL Schema, we have to define what is cal ...

  8. ios开发事件处理之 四:hittest方法的底层实现与应用

    #import "XMGWindow.h" /** 1:注意点:hitTest方法内部会调用pointInside方法,询问触摸点是否在自己身上,当遍历子控件时,传入的坐标点要进行 ...

  9. ios开发事件处理之:一:UIView的拖拽

    1.ios当中常⽤的事件?  触摸事件 ,加速计事件 ,远程控制事件 2.什么是响应者对象? 继承了UIResponds的对象我们称它为响应者对象 UIApplication.UIViewContro ...

  10. 让Apache 和nginx支持跨域訪问

    1,怎样让Apache支持跨域訪问呢? 步骤: 改动httpd.conf,windows中相应的文件夹是:C:\wamp\bin\apache\Apache2.4.4\conf\httpd.conf ...