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

  1. ZGBG
  2. X
  3. EE
  4. AAB
  5. AABA
  6. AABB
  7. BCBABCC
  8. *

Sample Output

  1. ZGBG is surprising.
  2. X is surprising.
  3. EE is surprising.
  4. AAB is surprising.
  5. AABA is surprising.
  6. AABB is NOT surprising.
  7. 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代码
  1. #include<cstdio>
  2. #include<cstring>
  3. char str[];
  4. int main(){
  5. int i,j,k;
  6. int count;
  7. int len;
  8. while(scanf("%s",str)==&&strcmp(str,"*")!=){
  9. len=strlen(str);
  10. if(len<=){
  11. printf("%s is surprising.\n", str);
  12. continue;
  13. }
  14. for(i=,count=;i<len&&count!=; i++){
  15. count=;
  16. for(j=i+,k=;j<len;j++,k++){
  17. if(str[j]==str[k]) count++;
  18. if(count==) break;
  19. }
  20. }
  21. if(count==)
  22. printf("%s is NOT surprising.\n", str);
  23. else
  24. printf("%s is surprising.\n", str);
  25. }
  26. return ;
  27. }

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. docker集群——介绍Mesos+Zookeeper+Marathon的Docker管理平台

    容器为用户打开了一扇通往新世界的大门,真正进入这个容器的世界后,却发现新的生态系统如此庞大.在生产使用中,不论个人还是企业,都会提出更复杂的需求.这时,我们需要众多跨主机的容器协同工作,需要支持各种类 ...

  2. Ubuntu14.04使用samba服务器共享Home目录

    这里记录一下,以Ubuntu 14.04为例.   1.安装samba服务器. sudo apt-get install samba 2.修改配置文件 sudo vim /etc/samba/smb. ...

  3. 【转】Python——编码规范

    来自于 啄木鸟社区 Python Coding Rule --- hoxide 初译 dreamingk 校对发布 040724 --- xyb 重新排版 040915 --- ZoomQuiet M ...

  4. 《深入理解jvm》笔记---第七章

    虚拟机类载入机制 1. 类的生命周期: 载入.验证.准备.解析.初始化.使用.卸载七个阶段.当中验证.准备.解析三个阶段统称为连接. 当中,解析的阶段的时机并不一定. 2. Java类载入的时机: J ...

  5. jquery 事件,注册 与重复事件处理

    jquery有时候会出现重复注册一个事件的问题,导致点击一个事件,这个事件被重复执行,也就是触发事件的次数有几次, 那么这个事件就会被执行叠加重复几次. 我这边做的一个项目,在某个页面初始化的时候,给 ...

  6. WireShake的使用

    转自点击打开链接 之前写过一篇博客:用 Fiddler 来调试HTTP,HTTPS. 这篇文章介绍另一个好用的抓包工具wireshark, 用来获取网络数据封包,包括http,TCP,UDP,等网络协 ...

  7. Python-PyQt安装

    Windows下安装PyQt需要使用 mingw32-make工具, 以在windows下make

  8. duplicate symbols 错误解决方案

    duplicate symbols for architecture arm64 after xCode 8.0 update 升级xcode 8以后提示有的变量重复了,只要在 Build setti ...

  9. destoon二次开发基础指南

    代码首先包含common.inc.php文件 在common.inc.php文件中,首先定义常量. define('IN_DESTOON', true); define('IN_ADMIN', def ...

  10. systemd 管理python 程序

    [Unit] Description = test-minapp After = network.target [Service] PermissionsStartOnly = true PIDFil ...