Gym 101158D(暴力)

题意:给定两个长度为N的字符串,1<=N<=4000,求满足字符串1中的某个区间所有的字母种类和个数都与字符串2中的某个区间相同最长的区间长度。

分析:

1、预处理每个串字母个数的前缀和。

2、暴力即可。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-12;
inline int dcmp(double a, double b)
{
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 4000 + 10;
const int MAXT = 3025 + 10;
using namespace std;
char s1[MAXN], s2[MAXN];
struct Node{
    int sum[27];
    bool operator < (const Node &rhs)const{
        for(int i = 0; i < 26; ++i){
            if(sum[i] != rhs.sum[i]) return sum[i] < rhs.sum[i];
        }
        return false;
    }
    Node operator - (const Node &rhs)const{
        Node tmp;
        for(int i = 0; i < 26; ++i){
            tmp.sum[i] = sum[i] - rhs.sum[i];
        }
        return tmp;
    }
}num1[MAXN], num2[MAXN];
set<Node> st;
int main(){
    scanf("%s%s", s1 + 1, s2 + 1);
    int len1 = strlen(s1 + 1);
    int len2 = strlen(s2 + 1);
    for(int i = 1; i <= len1; ++i){
        num1[i] = num1[i - 1];
        ++num1[i].sum[s1[i] - ‘a‘];
    }
    for(int i = 1; i <= len2; ++i){
        num2[i] = num2[i - 1];
        ++num2[i].sum[s2[i] - ‘a‘];
    }
    int len = min(len1, len2);
    bool ok = false;
    for(int i = len; i >= 1; --i){
        st.clear();
        for(int j = 1; j + i - 1 <= len1; ++j){
            st.insert(num1[j + i - 1] - num1[j - 1]);
        }
        for(int j = 1; j + i - 1 <= len2; ++j){
            if(st.count(num2[j + i - 1] - num2[j - 1])){
                ok = true;
                printf("%d\n", i);
                break;
            }
        }
        if(ok) break;
    }
    if(!ok) printf("0\n");
    return 0;
}

  

时间: 2024-08-06 18:25:31

Gym 101158D(暴力)的相关文章

很好的脑洞题:dfs+暴力 Gym - 101128A Promotions

http://codeforces.com/gym/101128 题目大意:给你一个a,b,e,p.有e个点,p条有向边,每条边为(x,y),表示x->y,每次我们都取出一个入度为0的,并且一次性取出来的个数为a(或b).当然,取出来的种类可能有很多种(即一个集合),问,这个集合中有多少个数字是相同的. 第一个输出集合长度为a的,第二个输出集合长度为b的,第三个输出无论如何都无法被取出的个数. 思路:建立正向图和反向图. 定义pair<int, int> interval[i] 表示第i

ACM: Gym 100935G Board Game - DFS暴力搜索

Board Game Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935G Description standard input/outputStatements Feras bought to his nephew Saleem a new game to help him learning calculating. The game consists of a boar

Codeforces Gym 100203G Good elements 暴力乱搞

原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑暴力的复杂度是O(n^3),所以我们需要记录所有的ai+aj,如果当前考虑到了ak,那么就去前面寻找ai,使得ak-ai是我们记录过的和.整个算法的复杂度O(n^2). 代码 #include<iostream> #include<cstring> #include<cstdio> #include<

Codeforces Gym 100286J Javanese Cryptoanalysis 傻逼暴力

原题地址:http://codeforces.com/gym/100286/attachments/download/2013/20082009-acmicpc-northeastern-european-regional-contest-neerc-08-en.pdf 此题题意是给你一个单对单密文,让你还原为原文,原文有个性质是,每个单词都是元音和辅音交替组成. 做法是直接5重for,暴力枚举AEIOU分别对应的字母,然后检查,然后输出 详见代码: //#include<iostream>

ACM: Gym 101047M Removing coins in Kem Kadr&#227;n - 暴力

Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Practice Description standard input/output Andréh and his friend Andréas are board-game aficionados. They know many of their friends

Gym 101055A 计算几何,暴力

http://codeforces.com/gym/101055/problem/A 题目:给定一些三维空间的点,要你找一个平面,能覆盖尽量多的点,只要求输出点数即可.n<=50 因为数据量小,我们考虑暴力. 首先,三个不在同一条直线的点,确定一个平面,然后枚举其他的点.判断一下,这样的复杂度是n^4.可以接受 特判.所有点都在同一条直线.直接输出n. 后面的,枚举三个点后,能算出这个平面的法向量,然后枚举其他点,与法向量的数量积是0的,就可以ans++ #include <cstdio>

Codeforces Gym 100002 C &quot;Cricket Field&quot; 暴力

"Cricket Field" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100002 Description Once upon a time there was a greedy King who ordered his chief Architect to build a field for royal cricket inside his park. The King was so

Codeforces Gym 100637G G. #TheDress 暴力

G. #TheDress Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/G Description After landing on planet i1c5l people noticed that blue and black clothes are quite popular among the locals. Each aboriginal has at least

Codeforces gym 100685 A. Ariel 暴力

A. ArielTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/A Description King Triton really likes watching sport competitions on TV. But much more Triton likes watching live competitions. So Triton decides to set up