字符串哈希——Friends(经典哈希应用)+D. Remove Two Letters(CF div3)

传送门:Friends

思路:枚举每一个位置的字母,求出删去该出字母后的字符串是否由两段相同字符串拼接而成的,同时要注意一种特殊的情况,题目里面没有说,但形如ABBBCABBC这种有三个位置都可以得到ABBCABBC这一个,他们是算一个。

其余就奇数直接out。

代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef unsigned long long ull;
const int pp=13331,N=2e6+10;
char a[N];
ull p[N];
ull last;
ull s[N];
int n;
ull get(ull a[],int l,int r)
{
    return a[r]-a[l-1]*p[r-l+1];
}
int main()
{
    cin>>n;
    scanf("%s",a+1);
    if(n%2==0)
    {
    printf("NOT POSSIBLE\n");
    return 0;
    }
    p[0]=1;
    for(int i=1;i<=n;i++)
    {
        s[i]=s[i-1]*pp+a[i];
        p[i]=p[i-1]*pp;
    }
    int sum=0;
    int ans;
    for(int i=1;i<=n;i++)
    {
            if(i==n/2+1)
        {
            if(get(s,1,n/2)!=last&&get(s,1,n/2)==get(s,n/2+2,n))
            {
                last=get(s,1,n/2);
            sum++;
            ans=i;
            }
        }else if(i<=n/2)
        {
            if(get(s,n/2+2,n)!=last&&get(s,n/2+2,n)==get(s,1,i-1)*p[n/2+1-i]+get(s,i+1,n/2+1))
               {
                   last=get(s,n/2+2,n);
                sum++;
               ans=i;
               }
        }else
        {
               if(get(s,1,n/2)!=last&&get(s,1,n/2)==get(s,n/2+1,i-1)*p[n-i]+get(s,i+1,n))
               {
                   last=get(s,1,n/2);
                sum++;
               ans=i;
               }
        }
        if(sum>1)
          break;
    }
    if(sum>1)
        puts("NOT UNIQUE");
    else if(sum==0)
        puts("NOT POSSIBLE");
    else{
        if(ans>=n/2+1)
        {
            for(int i=1;i<=n/2;i++)
            printf("%c",a[i]);
        }else
        for(int i=n/2+2;i<=n;i++)
            printf("%c",a[i]);

    }
    return 0;
}

传送门:Problem - D - Codeforces

思路:使用map<string,int>会爆内存,这样只能用map<ull,int>,存储每一个删除了两个字符之后的字符串的哈希值,这里除了求两段区间的哈希值还要求他们拼接起来后的才是真正的哈希值,这里字符串拼接不是简单的

// Problem: D. Remove Two Letters
// Contest: Codeforces - Codeforces Round #855 (Div. 3)
// URL: https://codeforces.com/contest/1800/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
typedef unsigned long long ull;
const int N = 2e5 + 10, pp = 13331;
int d[N];
char a[N];
int n, t, m;
ull p[N];
ull s[N];
ull get(ull a[], int l, int r) {
  // if (l > r) return 0;
  return a[r] - a[l - 1] * p[r - l + 1];
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    map<ull, int> mp;
    scanf("%d", &n);
    scanf("%s", a + 1);
    p[0] = 1;
    for (int i = 1; i <= n; i++) {
      s[i] = s[i - 1] * pp + a[i];
      p[i] = p[i - 1] * pp;
    }
    int ans = 0;
    ull tmp = pp * pp;
    for (int i = 1; i < n; i++) {
      ull x = get(s, i + 2, n) + get(s, 1, i - 1) * p[n - i - 1];
      //  cout << x << endl;
      if (mp[x] == 0) {
        ans++;
        mp[x]++;
      }
    }

    cout << ans << endl;
  }

  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值