CTU Open Contest 2019 G. Beer Mugs(异或前缀和 + 记忆化存储)

Beer Mugs
mugs.c, mugs.cpp, Mugs.java, mugs.py
Damian is a beer mug collector. His collection fills most of the shelves in his vintage wooden cabinet where all mugs are proudly displayed. The mugs are of various brands. There might be, and often are, more mugs of the same brand in the collection.
Mugs on a shelf in Damian’s collection always form a single symmetric row. Specifically, the symmetry of the row means that the sequence of particular mug brands in the row is the same when the mugs are being admired one by one from left to right and when the mugs are being admired one by one from right to left. There is still one empty shelf in the cabinet and Damian looks for an opportunity to fill it with a new set of mugs.
The widely recognized Mastodon brewery (admired for its Woolly Mammoth beer) organizes annually the so-called beer season. Participants of the season are engaged in daily beer brewing activities and are rewarded each day by a special collector mug. Each day in the season is assigned a particular mug brand. The mug brands for all days in the season are known in advance, some brands may appear repeatedly in the season.
A participant may subscribe for the whole season or just for a part of the season. However, all days of his or her participation have to be in one uninterrupted sequence of days, a participant cannot leave the season and then come back again after some days of absence.
Damian is keen to take part in the beer season. He decided that the set of mugs he brings home should be suited for his display without adding or removing any mug, and that the set should be as big as possible.
Given the list of mug brands provided by the brewery for all days in the beer season, find the size of the biggest set of mugs suitable for Damian’s display which can be obtained by subscribing to some appropriately chosen part of the beer season.
Input Specification
The first input line contains integer N (1 < N ≤ 300000) the number of days in the brewery beer season. The next line contains N characters representing the list of all beer mug brands offered in the season, day by day. The list is naturally ordered from the first day to the last day of the season. Each brand is coded by a single lower case letter, from ’a’ to ’t’. The list contains no blanks.
Output Specification
Print a single integer representing the size of the biggest set of mugs which Damian can bring home from the brewery beer season and which is suitable, without changes, for his collection display.
Sample Input 1
6

abcabc


Output for Sample Input 1
6


Sample Input 2
20

ghjahjghsajdjhlfslja


Output for Sample Input 2
7


Sample Input 3
12

aabbccddabcd


Output for Sample Input 3
9

 

题意:

寻找最长的重组后可以构成回文串的连续子串

思路:(来源于题解)

将 ' a ' - ' t '对应 2 ^ 0 --- 2 ^ 19,从左到右记录异或前缀和,用unordered_map存储不同前缀和的第一次的出现位置。

mp.count() 查看数据有没有出现过

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int N = 3e5 +10;

int main()
{
    int n;
    string s;
    while(~scanf("%d", &n))
    {
        getchar();
        getline(cin, s);
        s = '#' + s;
        ll ans = 0, sum = 0;
        unordered_map<ll, ll>mp;
        mp[0] = 0;
        for(int i = 1; i <= n; ++i)
        {
            sum ^= (1 << (s[i] - 'a'));
            if(!mp.count(sum))
            {
                mp[sum] = i;
            }
            else
            {
                ans = max(ans, i - mp[sum]);
            }
            for(char j = 'a'; j <= 't'; ++j)
            {
                ll tmp = 1 << (j - 'a');
                if(mp.count(tmp ^ sum))
                {
                    ans = max(ans, i - mp[tmp ^ sum]);
                }
            }
        }
        printf("%lld\n", ans);
        mp.clear();
    }
    return 0;
}

 

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 长得好看会提高面试通过率吗? #
4724次浏览 49人参与
# 离家近房租贵VS离家远但房租低,怎么选 #
16929次浏览 137人参与
# 巨人网络春招 #
11572次浏览 230人参与
# 沪漂/北漂你觉得哪个更苦? #
1690次浏览 41人参与
# 你的实习产出是真实的还是包装的? #
3283次浏览 55人参与
# 春招至今,你的战绩如何? #
16326次浏览 148人参与
# MiniMax求职进展汇总 #
25320次浏览 323人参与
# HR最不可信的一句话是__ #
1135次浏览 33人参与
# AI面会问哪些问题? #
1000次浏览 25人参与
# 你做过最难的笔试是哪家公司 #
1336次浏览 23人参与
# AI时代,哪个岗位还有“活路” #
3002次浏览 53人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152967次浏览 889人参与
# 简历第一个项目做什么 #
32209次浏览 364人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
8035次浏览 43人参与
# XX请雇我工作 #
51165次浏览 171人参与
# 简历中的项目经历要怎么写? #
311176次浏览 4273人参与
# 投格力的你,拿到offer了吗? #
178408次浏览 891人参与
# 你最满意的offer薪资是哪家公司? #
77024次浏览 375人参与
# AI时代,哪些岗位最容易被淘汰 #
64919次浏览 895人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
187657次浏览 1123人参与
# 你怎么看待AI面试 #
180917次浏览 1322人参与
# 正在春招的你,也参与了去年秋招吗? #
364434次浏览 2642人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务