Two strings(贪心)


题解:一道在别人口中很水的贪心题,然而在考试的时候就没有想到,只拿了50分。下文中a为第一个字符串,b为第二个字符串。

我们维护两个数组,一个是l[i],一个是r[i]。l[i]表示b[0...i]在a中按题意完全匹配(从前向后匹配)的最前的位置,r[i]表示b[i...lenb]按题意从后向前匹配的最后的位置。这两个数组其实具有单调性,可以O(len)求出。接着我们枚举i,表示b[0...i]留下来,假如有个j,满足l[i]<r[j],那么把b[i+1...j-1]移走是满足题目要求的。而且随着i的增大,可行的j只会向后移动!用指针一遍扫过即可。

#include<algorithm>
#include<fstream>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;

char c1[105020],c2[105020];
int l[105010],r[105010],len1,len2,x,y,z;

void Work()
{
    int p1=0,p2=0;
    while (p1<=len1&&p2<=len2)
    {
        while (p1<=len1&&c1[p1]!=c2[p2]) p1++;
        l[p2] = p1;
        p2++;  p1++;
    }
    p1=len1,p2=len2;
    while (p1>=0&&p2>=0)
    {
        while (p1>=0&&c1[p1]!=c2[p2]) p1--;
        r[p2] = p1;
        p2--;  p1--;
    }
}

int main()
{
    freopen("c.in","r",stdin);
    freopen("c.out","w",stdout);
    scanf("%s",c1);  scanf("%s",c2);
    len1 = strlen(c1)-1;  len2 = strlen(c2)-1;
    for (int i=0; i<=len2; i++)
    l[i] = len1+1;
    for (int i=0; i<=len2; i++)
    r[i] = -1;
    Work();
    z = len2;
    for (int i=0; i<=len2; i++)
    {
        int p = i+1;
        while (r[p]<=l[i]&&p<=len2) p++;
        if (l[i]>len1) break;
        if (z>p-i-1)
        {
            z=p-i-1;  x=i;  y=p;
        }
        if (z==0) break;
    }
    for (int i=0; i<=x; i++)
    printf("%c",c2[i]);
    for (int i=y; i<=len2; i++)
    printf("%c",c2[i]);
    return 0;
}
时间: 2024-08-01 15:10:24

Two strings(贪心)的相关文章

SPOJ:PATHETIC STRINGS(分配问题&amp;贪心)

Problem statement: A string is said to be “PATHETIC” if all the characters in it are repeated the same number of times. You are given a string of length n, what is the minimum number of changes required to make a string “PATHETIC”. The string has onl

HDU 6034 Balala Power!(贪心+排序)

Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1411    Accepted Submission(s): 239 Problem Description Talented Mr.Tang has n strings consisting of only lower case characters.

[贪心+模拟] zoj 3829 Known Notation

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5383 Known Notation Time Limit: 2 Seconds      Memory Limit: 65536 KB Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science.

LA 6979 Known Notation 构造+贪心 铜牌题

6979 Known NotationDo you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics andcomputer science. It is also known as postfix notation since every operator in an expression follows allof its operands. Bob is a stude

UVA 1368 DNA(模拟+贪心)

DNA (Deoxyribonucleic Acid) is the molecule which contains the genetic instructions. It consists of four different nucleotides, namely Adenine, Thymine, Guanine, and Cytosine as shown in Figure 1. If we represent a nucleotide by its initial character

URAL 2026 Dean and Schedule 贪心、双端队列(deque)、队列(queue)

C - Dean and Schedule Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 2026 Description A new academic year approaches, and the dean must make a schedule of classes for first-year students. Ther

hdu4915 Parenthese sequence 贪心O(n)解法

hdu4915 Parenthese sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 82    Accepted Submission(s): 25 Problem Description bobo found an ancient string. The string contains only three c

HDU4915:Parenthese sequence(贪心)

Problem Description bobo found an ancient string. The string contains only three charaters -- "(", ")" and "?". bobo would like to replace each "?" with "(" or ")" so that the string is valid (de

2014牡丹江区域赛K(贪心)ZOJ3829

Known Notation Time Limit: 2 Seconds      Memory Limit: 131072 KB Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expre