AtCoder Beginner Contest 284

A - Sequence of Strings

#include<bits/stdc++.h>
using namespace std;

int32_t main(){
    int n;
    cin >> n;
    vector<string> s(n);
    for( auto &i : s ) cin >> i;
    for( int i = n-1 ; i >= 0 ; i -- )
        cout << s[i] << "\n";
    return 0;
}

B - Multi Test Cases

#include<bits/stdc++.h>
using namespace std;

int read(){...}

void solve(){
    int n = read() , res = 0;
    for( int x ; n ; n -- ){
        x = read();
        if( x & 1 ) res ++;
    }
    cout << res << "\n";
    return;
}

int32_t main(){
    for( int T = read() ; T ; T -- )
        solve();
    return 0;
}

C - Count Connected Components

#include<bits/stdc++.h>
using namespace std;

int read(){...}

vector<int> vis;
vector<vector<int>> e;

void dfs( int u ){
    vis[u] = 1;
    for( auto v : e[u] ){
        if( vis[v] ) continue;
        dfs(v);
    }
    return;
}

int32_t main(){
    int n = read() , m = read() , res = 0;
    e.resize(n+1) , vis.resize(n+1);

    for( int u , v ; m ; m -- )
        u = read() , v = read() , e[u].push_back(v) , e[v].push_back(u);
    for( int i = 1 ; i <= n ; i ++ ){
        if( vis[i] ) continue;
        dfs(i) , res ++;
    }
    cout << res << "\n";
    return 0;
}

D - Happy New Year 2023(补题)

根据唯一分解定理,\(p^2q\)是唯一的,且\(max(p,q)\le \sqrt[3]{n}\)

所以直接暴力写就好

#include<bits/stdc++.h>
#define int unsigned long long
typedef long long ll;
using namespace std;

int read(){...}

void solve(){
    int n = read();
    for( int i = 2 , j ; ; i ++ ){
        if( n % i != 0 ) continue;
        n /= i;
        if( n % i == 0 ) j = n / i;
        else j = i , i = sqrt( n );
        cout << i << " " << j << "\n";
        return;
    }
}

int32_t main(){
    for( int T = read() ; T ; T -- ) solve();
    return 0;
}

E - Count Simple Paths

非常简单的统计一下从1开始的简单路径,dfs不断的搜索就好了。

#include<bits/stdc++.h>
using namespace std;

int read(){...}

const int N = 1e6 ;
int n , m , res;
vector<int> vis;
vector<vector<int>> e;

void dfs( int u ){
    vis[u] = 1;
    res ++;
    if( res == N ){
        cout << res<<"\n";
        exit(0);
    }
    for( auto v : e[u] ){
        if( vis[v] ) continue;
        dfs(v);
    }
    vis[u] = 0;
    return;
}


int32_t main(){
    n = read() , m = read() , res = 0;
    e.resize(n+1) , vis.resize(n+1);
    for( int u , v ; m ; m -- ){
        u = read() , v = read();
        e[u].push_back(v) , e[v].push_back(u);
    }
    dfs(1);
    cout << res << "\n";
    return 0;
}

F - ABCBAC(补题)

这题其实不太难,字符串哈希,把字符串正反都哈希一遍,然后枚举一下开头的长度,然后把开头和结尾的拼起来,在用反向哈希计算出中间部分倒过来的哈希值,然后比较一下就好了。

这题的坑点有两个,一是中间区间计算,二是卡单哈希,要用双哈希。

#include<bits/stdc++.h>

using namespace std;
#define int long long
#define mp make_pair
typedef long long ll;
typedef pair<int, int> hashv;


const hashv mod = mp( 1e9+7 , 998244353 );
const hashv base = mp(13331,23333);

hashv operator + ( hashv a , hashv b ) {
    int c1 = a.first + b.first , c2 = a.second + b.second;
    if( c1 >= mod.first ) c1 -= mod.first;
    if( c2 >= mod.second ) c2 -= mod.second;
    return mp( c1 , c2 );
}

hashv operator - ( hashv a , hashv b ) {
    int c1 = a.first-b.first , c2 =a.second-b.second;
    if( c1 < 0 ) c1 += mod.first;
    if( c2 < 0 ) c2 += mod.second;
    return mp( c1 , c2 );
}

hashv operator * ( hashv a , hashv b ) {
    return mp( a.first*b.first%mod.first , a.second*b.second%mod.second );
}

const int N = 2e6+5;

vector< hashv > p , hs , ht;

void hashStr( const string &s , vector<hashv> &v ){
    v.resize(s.size()+1);
    for( int i = 1 ; i <= s.size() ; i ++ )
        v[i] = v[i-1] * base + mp( s[i-1] , s[i-1] );
    return;
}

hashv getHash( int l , int r , const vector<hashv> &v){
    if( l > r ) return mp( 0 , 0 );
    return v[r] - v[l-1] * p[r-l+1];
}
void init( int n ){
    p = vector<hashv>( n+1 ) , p[0] = mp(1,1);
    for( int i = 1 ; i <= n ; i ++ ) p[i] = p[i-1] * base;
}

int32_t main() {
    int n , m;
    string s , t;
    cin >> n >> s;
    m = n*2 , t = s , reverse(t.begin(),t.end());
    init(n) , hashStr( s , hs ) , hashStr( t , ht );

    for( int i = 0 ; i <= n ; i ++ ){
        hashv s1 = getHash( 1 , i , hs ) * p[n-i] + getHash( i+n+1 , m , hs );
        hashv s2 = getHash(  n-i+1 , m-i , ht);
        if( s1 == s2 ){
            for( int j = 1 ; j <= i ; j ++ ) printf("%c" , s[j-1] );
            for( int j = i+n+1 ; j <= m ; j ++ ) printf("%c" , s[j-1] );
            printf("\n%d\n" , i );
            return 0;
        }
    }
    printf("-1\n");
    return 0;
}
posted @ 2023-01-09 10:55  PHarr  阅读(40)  评论(0编辑  收藏  举报