pair 和 map結合應用——POJ 3096

wxyfennie發表於2016-06-08

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]++;

Time Limit: 1000MS   Memory Limit: 65536K
Total 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

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.

題目解法:

按照間隔距離遍歷

{

將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;
    }



}


相關文章