欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

AOJ1370: Hidden Anagrams(hash)

程序员文章站 2022-10-07 07:51:53
题意 "题目链接" Sol 直接对出现的次数hash即可,复杂度$O(26n^2)$ 一开始没判长度条件疯狂wa cpp include // define int long long define LL long long define ull unsigned long long using n ......

题意

sol

直接对出现的次数hash即可,复杂度\(o(26n^2)\)

一开始没判长度条件疯狂wa

#include<bits/stdc++.h> 
//#define int long long 
#define ll long long 
#define ull unsigned long long 
using namespace std;
const int maxn = 4001, mod = 1e9 + 7, inf = 1e9 + 10;
const double eps = 1e-9;
template <typename a, typename b> inline bool chmin(a &a, b b){if(a > b) {a = b; return 1;} return 0;}
template <typename a, typename b> inline bool chmax(a &a, b b){if(a < b) {a = b; return 1;} return 0;}
template <typename a, typename b> inline ll add(a x, b y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename a, typename b> inline void add2(a &x, b y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename a, typename b> inline ll mul(a x, b y) {return 1ll * x * y % mod;}
template <typename a, typename b> inline void mul2(a &x, b y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename a> inline void debug(a a){cout << a << '\n';}
template <typename a> inline ll sqr(a x){return 1ll * x * x;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int n, m;
char s1[maxn], s2[maxn];
map<ull, bool> mp;
ull base = 29;
int num[27];
ull get() {
    ull now = 0;
    for(int i = 0; i < 26; i++) 
        now = now * base + num[i];
    return now;
}
signed main() {
    scanf("%s", s1 + 1);
    scanf("%s", s2 + 1);
    n = strlen(s1 + 1); m = strlen(s2 + 1);
    for(int len = min(n, m); len >= 1; len--) {
        mp.clear();
        memset(num, 0, sizeof(num));
        for(int i = 1; i <= n; i++) {
            num[s1[i] - 'a']++;
            if(i > len) num[s1[i - len] - 'a']--;
            if(i >= len) mp[get()] = 1;
        }
        memset(num, 0, sizeof(num));
        for(int i = 1; i <= m; i++) {
            num[s2[i] - 'a']++;
            if(i > len) num[s2[i - len] - 'a']--;
            if(i >= len && mp[get()]) {
                cout << len << '\n';
                return 0;
            }
        }
    }
    puts("0");
    return 0;
}