[ACM] POJ 3096 Surprising Strings (map使用)
| 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使用)的更多相关文章
- POJ 3096 Surprising Strings
Surprising Strings Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5081 Accepted: 333 ...
- 【字符串题目】poj 3096 Surprising Strings
Surprising Strings Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6193 Accepted: 403 ...
- POJ 3096 Surprising Strings(STL map string set vector)
题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...
- POJ 3096:Surprising Strings
Surprising Strings Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6258 Accepted: 407 ...
- [POJ3096]Surprising Strings
[POJ3096]Surprising Strings 试题描述 The D-pairs of a string of letters are the ordered pairs of letters ...
- C - Surprising Strings
C - Surprising Strings 题意:输入一段字符串,假设在同一距离下有两个字符串同样输出Not surprising ,否 ...
- HDOJ 2736 Surprising Strings
Surprising Strings Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 2736 Surprising Strings
Surprising Strings Time Limit:1000MS Memory Limit:65536KB 64 ...
- poj 2406 Power Strings 后缀数组解法
连续重复子串问题 poj 2406 Power Strings http://poj.org/problem?id=2406 问一个串能否写成a^n次方这种形式. 虽然这题用kmp做比较合适,但是我们 ...
随机推荐
- 搭建hbase-0.94.26集群环境 分类: B7_HBASE 2015-01-24 17:14 861人阅读 评论(0) 收藏
先安装hadoop1.2.1,见http://blog.csdn.net/jediael_lu/article/details/38926477 1.配置hbase-site.xml <prop ...
- "网络适配器本地连接没有有效ip地址配置"错误的解决办法
作者:朱金灿 来源:http://blog.csdn.net/clever101 win server2008通过一台win xp的共享网络来上网,采用ipv4,网址设置如下: win xp是可以上网 ...
- https://www.cyberciti.biz/faq/howto-change-rename-user-name-id/
https://www.cyberciti.biz/faq/howto-change-rename-user-name-id/
- php实现求字符串第一个只出现一次的字符
php实现求字符串第一个只出现一次的字符 一.总结 很简单的逻辑 1.两个数组,一个存字母,一个存字母出现的次数 二.php实现求字符串第一个只出现一次的字符 题目描述 在一个字符串(1<=字符 ...
- js如何实现动态显示表格数据(点奇数显示奇数单元格内容)
js如何实现动态显示表格数据(点奇数显示奇数单元格内容) 一.总结 一句话总结: 1.动态指定表格中每个单元格的id,然后通过id可以获取每个单元格,然后对里面的innerHTML进行赋值. 2.弄了 ...
- 代码中jndi数据源的支持
项目中基本都使用Spring框架,支持jndi还是很简单的,只需在spring配置文件中加入 <!-- 使用jndi配置数据源 --> <bean id="dataSour ...
- OSGi开发环境的建立
1 OSGi开发环境的建立 1.1 Equinox是什么 从代码角度来看,Equinox其实就是OSGi核心标准的完整实现,并且还在这个基础上增加了一些额外的功能(比如为框架增加了命令行和程序执行的入 ...
- Web开发标配--开发人员工具-F12
喜欢从业的专注,七分学习的态度. 360浏览器-开发工具 谷歌-开发工具 IE-开发工具 Web开发中最最烦琐的莫过于调整CSS和JS,而最方便最高效的方式就是利用浏览器的开发工具调整.CSS可以实时 ...
- 为什么java的web开发中URLEncoder.encode方法要为什么要调用两次
一: 我们先看2个编码的情况 String name=java.net.URLEncoder.encode("测试", "UTF-8"); System.out ...
- 云主机启动Node服务后,关闭控制台,无法访问的问题
之前一直用node app.js操作,开启服务后,关闭控制台,仍然可以正常访问我的网站.但昨晚新买腾讯云的服务器后,发现关闭控制台后,就无法访问网站了.然后给腾讯云发了个工单.腾讯云的工程师给了一篇技 ...