首页 > 其他分享 >acwing 我在哪?(字符串哈希)

acwing 我在哪?(字符串哈希)

时间:2023-02-15 22:26:19浏览次数:57  
标签:set int mid 哈希 ans 字符串 include unordered acwing

原题链接

image

题解

分析

  • 设答案为ans,那么大于ans,肯定不成立,小于ans成立,这符合二分答案的特点
  • 然后使用unordered_set和substr进行查重
  • substr:第一个参数为开始项,第二个参数为要截取的长度

代码

#include "iostream"
#include "string"
#include "unordered_set"
using namespace std;

int n;
string str;
bool check(int mid){
    unordered_set<string>hash;
    for(int i=0;i+mid<=n;i++){
        string s=str.substr(i,mid);
        if(hash.count(s))return false;
        hash.insert(s);
    }
    return true;
}
int main(){
    cin>>n>>str;
    int l=1,r=n;
    while (l<r){
        int mid=(l+r)>>1;
        if(check(mid))r=mid;
        else l=mid+1;
    }
    cout<<r;
}

标签:set,int,mid,哈希,ans,字符串,include,unordered,acwing
From: https://www.cnblogs.com/ChengMao/p/17124936.html

相关文章

  • 字符串类型
    字符串操作vars,s1,s2:String;begins:='microsoftisabigCompany';s2:=Trimleft(s);//s2的内容为'microsoftisabigCompany's2:=TrimRig......
  • acwing 改变数组元素
    原题链接分析前缀和和差分算法,能够用线性的时间解决问题,将时间复杂度降为O(n)比如这道题,如果暴力的话,每次进行ai操作的时候都要进行循环,效率很低一般题里只要说将某......
  • 字符串方法
    >>>spam='Helloworld'>>>spam.upper()#所有字母被转为大写'HELLOWORLD'>>>spam.lower()#所有字母被转为小写'helloworld'>>>spam#未改变原字符串'......
  • APS.NET Core 6.0Json任何类型读取到字符串属性The JSON value could not be converte
    在升级.netsdk到6.0版本后出现TheJSONvaluecouldnotbeconvertedtoSystem.String.原因是我代码定义的类型是string,但是传参的时候写了int,publicoverridevoidC......
  • 判断字符串是否与变量相符
    判断字符串是否与变量相符利用Equals()来判断字符串是否与变量一致usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassMa......
  • Java判断两个字符串(对象是否相等)
    Java判断两个字符串(对象是否相等)在Java中,常见的判断两个字符串(对象)是否相等的方法有两个,一个是.equals()方法,还有一个是"=="操作符,这两个的主要区别如下:.equals()方法比......
  • 字符串问题选讲
    [国家集训队]最长双回文串Manacher板子题,先跑出每个点为中心的最长回文串,然后求出每个点为左右端点的最长回文串,之后枚举分界点统计答案即可。submission「JZOI-1」拜......
  • 字符串常用类及常量池和扩容机制理解
    字符串相关类:String、StringBuffer、StringBuilder  字符串相关的类:* 1.String字符串类,底层是基于常量char[],一旦创建长度就固定不变了,适用于字符串不经常增删改的......
  • MongoDB连接字符串的URI格式
    两种的连接字符串格式1.标准的连接格式mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]](1)单机连接格式mongodb://user......
  • hex转string,hex转字符串,hex16进制转字符串,hex转中文
    hex转字符串,hex16进制转字符串,在线工具 https://www.toolscat.com/decode/hexhex转字符串,hex转string,string转hex,16进制转字符串,hex转字符串在线工具,hex转str在线......