Codeforces Round #612 (Div. 2)

A

A后面连续的P最长有几个
O(n)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define ll long long
using namespace std;
const int maxn = 1e5+5;
const int inf = 0x3f3f3f3f;
int main(){
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        char s[maxn];
        scanf("%s",s);
        int maxx = 0;
        for(int i = 0; i < n; i++){
            if(s[i] == 'A'){
                int xx = 0;
                for(int j = i + 1; j < n; j++){
                    if(s[j] == 'P')xx++;
                    else break;
                }
                maxx = max(maxx,xx);
                i += xx;
            }
        }
        cout << maxx << endl;
    }
    return 0;
}

B

对于O(n^3)的算法,需要进行优化,采用遍历O(n^2)然后按照条件求得需要的值,再进行查找给定的有没有这个值即可 优化为O(\(n^{2}log_{2}n\))
对于O(n^2)的算法,需要进行优化,采用遍历O(n)然后按照条件求得需要的值,再进行查找给定的有没有这个值即可O(\(nlog_{2}n\))
对于查找:如果是有序对,用二分,lower_bound(),upper_bound(),如果不是有序,可以set,map进行查找O(\(log_{2}n\))

3个一组,要么都相同,要么都不同
当k=1时
SSS
TTT
EEE
SET
这四种情况都可以组成一组
那么只要遍历O(n^2)找出需要的,然后看给的串里是否出现过这个串即可
小心全重复的情况

4 2
SS
SS
SS
SS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
const int maxn = 1505;
std::map<string, int> ma;
string x[maxn];
int main(){
    int n,k;
    cin >> n >> k;
    for(int i = 0; i < n; i++){
        cin >> x[i];
        ma[x[i]]++;
    }
    int ans = 0;
    for(int i = 0; i < n; i++){
        for(int j = i + 1; j < n; j++){
            string cur;
            for(int z = 0; z < k; z++){
                if(x[i][z] == x[j][z])cur += x[i][z];
                if(x[i][z] == 'S' && x[j][z] == 'E')cur += 'T';
                if(x[i][z] == 'S' && x[j][z] == 'T')cur += 'E';
                if(x[i][z] == 'E' && x[j][z] == 'S')cur += 'T';
                if(x[i][z] == 'E' && x[j][z] == 'T')cur += 'S';
                if(x[i][z] == 'T' && x[j][z] == 'E')cur += 'S';
                if(x[i][z] == 'T' && x[j][z] == 'S')cur += 'E';
            }
            ans += ma[cur];
            if(cur == x[i])ans--;//避免重复
            if(cur == x[j])ans--;
        }
    }
    printf("%d\n",ans/3);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Emcikem/p/12154679.html
今日推荐