C++的标准模版库的应用

Surprising Strings
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6625   Accepted: 4309

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

题解:
给一个字符串,要求,对于这个字符串空隔为k取字符对(k=0,1,2,3,4...)要求在相同的空隔取对过程汇总,整个字符串中没有一个相同字符对如:
ZGBZ:
间隔为0的字符对有: ZG、GB、BZ,三个均不相同
间隔为1的字符对有: ZG、 GZ,均不相同
间隔为2的字符对有: ZZ 仅有一个,不必比较。
这种字符串定义为"surprising".
之后按照格式输出。
 
看到这个题目,一开始一点思路都没有,不过从样例可知,单个字符和两个字符的情况下,不用比较,直接定义为“surprising”。如果用纯模拟,就是分别去不同的隔断,然后,分别取出字符对来进行比较,这种是纯暴力的方法;之后想到了,或许可以按照某一种方式来对字符串进行错位后的字符进行比较,就可以每一次比较到从头到尾,每一个字符在多种间隔的情况下是否存在相同,看如下解释:
第一次错位,错位量为1:
 

 
上下比较,仅有一对“C”相同;
第二次错位,错位量为2:
 

 
发现有两列相同,两个BB,则我们可以知道有:
两个字符对是相等的BB、BB。
 
即是说,我们每次循环一次,从头到尾分别取到间隔从0到m的字符对,只要有两次相同,我们就可以认为他是一个字符对相同,然后退出循环,输出。
直到所有的循环结束,还没有找到相同的字符对,我们才认为这个是“surprising”。
AC代码
#include<cstdio>
#include<cstring>
char str[];
int main(){
int i,j,k;
int count;
int len;
while(scanf("%s",str)==&&strcmp(str,"*")!=){
len=strlen(str);
if(len<=){
printf("%s is surprising.\n", str);
continue;
}
for(i=,count=;i<len&&count!=; i++){
count=;
for(j=i+,k=;j<len;j++,k++){
if(str[j]==str[k]) count++;
if(count==) break;
}
}
if(count==)
printf("%s is NOT surprising.\n", str);
else
printf("%s is surprising.\n", str);
}
return ;
}

poj3096的更多相关文章

  1. [POJ3096]Surprising Strings

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

  2. POJ3096:Surprising Strings(map)

    http://poj.org/problem?id=3096 for循环真是奇妙! #include <string.h> #include <stdio.h> #includ ...

  3. poj分类 很好很有层次感。

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

  4. 【转】POJ题目分类推荐 (很好很有层次感)

    OJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094)初期: 一. ...

  5. 【转】ACM训练计划

    [转] POJ推荐50题以及ACM训练方案 -- : 转载自 wade_wang 最终编辑 000lzl POJ 推荐50题 第一类 动态规划(至少6题, 和 必做) 和 (可贪心) (稍难) 第二类 ...

  6. POJ 题目分类(转载)

    Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...

  7. (转)POJ题目分类

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  8. acm常见算法及例题

    转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题  初期:一.基本算法:     (1)枚举. (poj17 ...

  9. poj分类

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

随机推荐

  1. BZOJ 1016 JSOI 2008 最小生成树计数 Kruskal+搜索

    题目大意:给出一些边,求出一共能形成多少个最小生成树. 思路:最小生成树有非常多定理啊,我也不是非常明确.这里仅仅简单讲讲做法.关于定各种定理请看这里:http://blog.csdn.net/wyf ...

  2. Spring 对没有实现接口的类使用aspect的时候,可以使用CGLIB

    AspectJ中使用CGLIB 在spring中,当对没有实现接口的类使用aspect的时候,可以使用CGLIB, <aop:aspectj-autoproxy proxy-target-cla ...

  3. mysql无法启动ERROR! MySQL is running but PID file could not be found ?

    转载于:http://blog.csdn.net/wuzhilon88/article/details/17616635 第一种方法:可能是硬盘满了,清理下垃圾文件. 第二种: 查看下数据库运行状态 ...

  4. lucene 查询

    csdn blog - Lucene 3.0 的Query Parser(查询语法)   ibm developerWorks - 使用 Apache Lucene 2.4.1 搜索文本   osch ...

  5. JPA学习笔记1——JPA基础 (转自CSDN)

    http://blog.csdn.net/chjttony/article/details/6086298 1.JPA简介: Java持久化规范,是从EJB2.x以前的实体Bean(Entity be ...

  6. spring学习笔记(五)

    1.后置通知 需求:调用相应业务方法后,完成资源的关闭. a. 在beans.xml中配置 .... <beans> <!--配置被代理对象--> <bean id=&q ...

  7. 转:介绍一个好用的抓取dump的工具-ProcDump

    介绍一个好用的抓取dump的工具-ProcDump Procdump是一个轻量级的Sysinternal团队开发的命令行工具, 它的主要目的是监控应用程序的CPU异常动向, 并在此异常时生成crash ...

  8. 学习使用master.dbo.spt_values表

    如果要生成的临时表中有个连续的数字列,或者连续的日期列,如下所示: 2012-1-1 2012-1-2 2012-1-3 ... ... 可以这样写: declare @begin datetime, ...

  9. 采用CDN加速后,如何在程序里获取用户IP地址

    现在很多网站用了CDN技术,但采用CDN技术后,原来用来获取访问源的IP地址的程序已不能正常使用,它拿到的并不是访问源的真实IP地址,而是CDN节点的IP地址,解决方法是对获取IP的代码作一点小改动. ...

  10. CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\

    解决方法: 1:设置 C:\windows\temp 文件夹安全权限  添加用户 NETWORK SERVICE  写入和读取权限 2:设置 C:\windows\temp 文件夹安全权限  添加用户 ...