pair 和 map结合应用——POJ 3096

来源:互联网 发布:微信点餐系统源码下载 编辑:程序博客网 时间:2024/06/07 15:21

pair用法:

1、定义

pair<第一个参数类型,第二个参数类型>变量名;

eg: pair<char,char>a;

2、赋值

a = make_pair("a","b");

map用法:

1、定义

map<第一个参数类型(键),第二个参数类型(值)>

eg: map<pair<char,char>,int>m;

2、赋值

m[键] = 值;

eg: m[a]++;

Surprising Strings
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6613 Accepted: 4302

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

ZGBGXEEAABAABAAABBBCBABCC*

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.

题目解法:

按照间隔距离遍历

{

将map清空

按照字符串长度遍历

{

每种出现过的<键,值>对都放在pair_a中;

记录每种<键,值>对出现自出map[a]++;

}

判断是否有map[a]的值大于等于2

如果有就不是surprising

没有就是 surprising

}

#include<iostream>#include<stdio.h>#include<math.h>#include<string.h>#include<stdlib.h>#include<algorithm>#include<queue>#include<vector>#include<map>using namespace std;char s[105];pair<char,char>a;int flag;map<pair<char,char>,int>m;int main(){    while(scanf("%s",s)!=EOF)    {        if(s[0]=='*'&&strlen(s)==1)break;        int len = strlen(s);        int k = 0;            for(int j = 1 ; j < len;j++)            {                m.clear();                for(int i = 0 ; i <len-j;i++)                {                    k++;                    a = make_pair(s[i],s[i+j]);                    m[a]++;                }                flag = 0;                for(int i = 0 ; i < len;i++)                {                     a = make_pair(s[i],s[i+j]);                    if(m[a]>=2)                    {                        flag = 1;                        break;                    }                }            if(flag)                {cout<<s<<" "<<"is NOT surprising."<<endl;break;}            }        if(!flag)            cout<<s<<" is surprising."<<endl;    }}


0 0
原创粉丝点击