zoukankan      html  css  js  c++  java
  • CodeForces 91A Newspaper Headline

    题目链接:CodeForces - 91A  Newspaper Headline

    官方题解:

    In this problem letters from s1 should be taken greedily: take the left letter from the right of the last used letter, if there is no necessary letter from the right of the right used letter the the search should be started from the beginning of string s1 and the answer should be increased by one. But the brute solution get TL and have complexity O(Ans * |s1|).
    This solution can be optimized using the following way. For every position in s1 let's precalculate positions of the closest letters from the right of it from the alphabet. It can be done by moving from the right to the left ins s1 and remembering the last position of every type of symbol. This solution have complexity O(|s1| * K + |s2|), where K is a size of alphabet.

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<string>
    #include<cmath>
    #define CLR(a,b) memset((a),(b),sizeof((a)))
    using namespace std;
    
    const int N = 10005;
    const int M = 1e6+5;
    
    char s1[N], s2[M];
    int d[N][26];
    bool vis[26];
    
    int main () {
        scanf("%s %s", s1, s2);
        int i, j;
        CLR(d, -1);
        CLR(vis, 0);
        int len1 = strlen(s1);
        int len2 = strlen(s2);
        for(i = 0; i < len1; ++i) {
            vis[ s1[i]-'a' ] = 1;
            for(j = 0; j <= i; ++j) {
                if(d[j][ s1[i]-'a' ] == -1)
                    d[j][ s1[i]-'a' ] = i;
            }
        }
        for(j = 0; j < len2; ++j) {
            if(vis[ s2[j]-'a' ] == 0) {
                printf("-1
    "); return 0;
            }
        }
        int ans = 0;
        for(j = 0; j < len2; ) {
            i = 0;
            while(i < len1 && j < len2 && d[i][ s2[j]-'a' ] != -1) {
                i = d[i][ s2[j]-'a' ] + 1;
                j++;
            }
            ans++;
        }
        printf("%d
    ", ans);
        return 0;
    }
  • 相关阅读:
    Ceph 之RGW Cache
    Ceph 之RGW Pub-Sub Module
    Ceph 之RGW Data Layout
    RocksDB 之Write Ahead Log(WAL)
    Ceph 之 Background on http frontends
    Ceph 之Multisite 下的bucket reshard
    Ceph之PG数调整
    Ceph之对象存储网关RADOS Gateway(RGW)
    window mysql重启、忘记密码等操作
    selenium处理HTML5视频播放未能自动播放解决办法
  • 原文地址:https://www.cnblogs.com/GraceSkyer/p/6670584.html
Copyright © 2011-2022 走看看