[ACM] POJ 3096 Surprising Strings (map使用)

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

Mid-Central USA 2006

解题思路:

推断一个字符串是不是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;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

时间: 2024-11-09 02:12:19

[ACM] POJ 3096 Surprising Strings (map使用)的相关文章

[ACM] POJ 3096 Surprising Strings (map的使用)

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

POJ 3096 Surprising Strings

Surprising Strings Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7225   Accepted: 4663 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

POJ训练计划3096_Surprising Strings(STL/map)

解题报告 题目传送门 题意: 给一个字符串,要求,对于这个字符串空隔为k取字符对(k=0,1,2,3,4...)要求在相同的空隔取对过程汇总,整个字符串中没有一个相同字符对如: ZGBZ: 间隔为0的字符对有: ZG.GB.BZ,三个均不相同 间隔为1的字符对有: ZG. GZ,均不相同 间隔为2的字符对有: ZZ 仅有一个,不必比较. 这种字符串定义为"surprising". 之后按照格式输出. 思路: map暴力. #include <iostream> #inclu

[ACM] POJ 2947 Widget Factory (高斯消元)

Widget Factory Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 4436   Accepted: 1502 Description The widget factory produces several different kinds of widgets. Each widget is carefully built by a skilled widgeteer. The time required to

[ACM] POJ 1936 All in All (查找,一个串是否在另一个串中)

All in All Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27521   Accepted: 11266 Description You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever

poj 2406 Power Strings 后缀数组解法

连续重复子串问题 poj 2406 Power Strings http://poj.org/problem?id=2406 问一个串能否写成a^n次方这种形式. 虽然这题用kmp做比较合适,但是我们还是用后缀数组做一做,巩固后缀数组的能力. 对于一个串,如果能写出a^n这种形式,我们可以暴力枚举循环节长度L,那么后缀suffix(1)和suffix(1 + L)的LCP应该就是 lenstr - L.如果能满足,那就是,不能,就不是. 这题的话da算法还是超时,等我学了DC3再写上来. 其实这

C - Surprising Strings

                               C - Surprising Strings 题意:输入一段字符串,假设在同一距离下有两个字符串同样输出Not surprising ,否则输出surprising. 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-uniq

HDU 2736 Surprising Strings

Surprising Strings Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u 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-

poj 2406 Power Strings KMP匹配

对于数组s[0~n-1],计算next[0~n](多计算一位). 考虑next[n],假设t=n-next[n],如果n%t==0,则t就是问题的解,否则解为1. 这样考虑: 比如字符串"abababab", a  b a b a b a b * next     -1 0 1 2 3 4 5 6  7 考虑这样的模式匹配,将"abababab#"当做主串,"abababab*"当做模式串,于是进行匹配到n(n=8)时,出现了不匹配: 主串