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. css3-10 css3中的边框样式有哪几种

    css3-10 css3中的边框样式有哪几种 一.总结 一句话总结:1.border-radius 2. box-shadow 3.border-image三种,box一种border两种 1.css ...

  2. 驱动程序调试方法之printk——printk的原理与直接使用

    1.基本原理 (1)在UBOOT里设置console=ttySAC0或者console=tty1 这里是设置控制终端,tySAC0 表示串口, tty1 表示lcd(2)内核用printk打印 内核就 ...

  3. 摘录-MYSQL5.7版本sql_mode=only_full_group_by问题

    下载安装的是最新版的mysql5.7.x版本,默认是开启了 only_full_group_by 模式的,但开启这个模式后,原先的 group by 语句就报错,然后又把它移除了. 一旦开启 only ...

  4. [Git] How to rename your remote branch

    Rename your local foo branch with bar: git branch -m foo bar Remember this will add the new branch w ...

  5. Redis使用文档一

    1 Redis概述 1.1前言 Redis是一个开源.支持网络.基于内存亦可持久化的日志型.键值对存储数据库.使用ANSI C编写.并提供多种语言的API. 其开发由VMware主持,是最流行的键值对 ...

  6. [ES6] Use ES6 Proxies

    A JavaScript Proxy allows you to intercept operations performed on objects, arrays, or functions lik ...

  7. js把其他类型转化成字符串

    js把其他类型转化成字符串 一.总结 一句话总结:类型转换中的强制类型转换分为类型转换函数和类型名强制.js后一种和其它语言不同,是类型类的构造方法.String() 二.js把其他类型转化成字符串 ...

  8. CSU1323: ZZY and his little friends

    Description zzy养了一只小怪兽和N只凹凸曼,单挑的话每只凹凸曼都不是小怪兽的对手,所以必须由两只凹凸曼合作来和小怪兽战斗.凹凸曼A和凹凸曼B合作的战斗力为他们战斗力的异或值.现在由zzy ...

  9. Jquery前端分页插件pagination使用

    插件描述:JqueryPagination是一个轻量级的jquery分页插件.只需几个简单的配置就可以生成分页控件.并且支持ajax获取数据,自定义请求参数,提供多种方法,事件和回调函数,功能全面的分 ...

  10. 数组[0]和[firstobject]的区别

    数组[0]和[firstobject]的区别   [0]:数组为空时回报错 [firstobject]:数组为空时回返回nil