E. Mo的游戏

题目链接:https://www.hpuoj.com/contest/16/problem/E/

E. Mo的游戏

单测试点时限: 1.0 秒

内存限制: 512 MB

Mo翻书看到一种新的连连看游戏:对于一个字符串来说,只有当两个字符相同时候才可以进行相连,得分为字符串的长度减去两个相连字符的距离(如果整个字符串中某一种字符个数为1,那么不能相连故得分为0)。Mo现在在玩这个游戏,但是Mo不知道该选择哪一种字符进行相连,所以请你列出每种字符相连可以获得的最大分数,以此来让Mo进行参考。

输入

一个字符串𝑠 (0<𝑠𝑡𝑟𝑙𝑒𝑛(𝑠)<106, 𝑠中只包含大小写字符)。

输出

针对𝑠中出现过的字符,每行输出一个整数表示用当前字符进行相连可以获得的最大分数, 按照𝑎𝑧𝐴𝑍的顺序。具体格式参见样例。

样例

input
Aabcaabcb
output
a:8
b:7
c:5
A:0

这题也是模拟,只要注意第一次出现的位置,然后后面再出现比它距离更短的就更新

这里我们先把距离设置为INF,

 

//
//  main.cpp
#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;
#define ll long long
const int INF=0x3f3f3f3f;
int res[100000];
int pos[100000];
char s[1000000];

int main()
{
    memset(pos,-1,sizeof(pos));
    memset(res,INF,sizeof(res));
    cin >> s;
    int len = strlen(s);
    map<char,int>mp;
    for (int i=0; i<len; i++) {
        mp[s[i]]++;
        if (pos[s[i]] >= 0) {
            res[s[i]] = min(i-pos[s[i]],res[s[i]]);
        }
        pos[s[i]] = i;
    }
    for (char i='a'; i<='z'; i++) {
        if (mp[i]) {
            printf("%c:",i);
            if (res[i] != INF) printf("%d",len-res[i]);
            else printf("0");
            printf("\n");
        }
    }
    for (char i='A'; i<='Z'; i++) {
        if (mp[i]) {
            printf("%c:",i);
            if (res[i] != INF) printf("%d",len-res[i]);
            else printf("0");
            printf("\n");
        }
    }
    return 0;
}

 

posted @ 2019-04-01 17:46  codeSJCHEN  阅读(170)  评论(0编辑  收藏  举报