CF1200E Compress Words

题目描述

Amugae has a sentence consisting of nn words. He want to compress this sentence into one word. Amugae doesn't like repetitions, so when he merges two words into one word, he removes the longest prefix of the second word that coincides with a suffix of the first word. For example, he merges "sample" and "please" into "samplease".

Amugae will merge his sentence left to right (i.e. first merge the first two words, then merge the result with the third word and so on). Write a program that prints the compressed word after the merging process ends.

输入格式

The first line contains an integer nn ( 1 \le n \le 10^51≤n≤105 ), the number of the words in Amugae's sentence.

The second line contains nn words separated by single space. Each words is non-empty and consists of uppercase and lowercase English letters and digits ('A', 'B', ..., 'Z', 'a', 'b', ..., 'z', '0', '1', ..., '9'). The total length of the words does not exceed 10^6106 .

输出格式

In the only line output the compressed word after the merging process ends as described in the problem.

题意翻译

Amugae有n个单词,他想把这个n个单词变成一个句子,具体来说就是从左到右依次把两个单词合并成一个单词.合并两个单词的时候,要找到最大的i(i\ge 0)i(i≥0),满足第一个单词的长度为ii的后缀和第二个单词长度为ii的前缀相等,然后把第二个单词第ii位以后的部分接到第一个单词后面.输出最后那个单词

输入输出样例

输入 #1复制

5
I want to order pizza

输出 #1复制

Iwantorderpizza

输入 #2复制

5
sample please ease in out

输出 #2复制

sampleaseinout

一开始用了string类,感觉按道理没问题,但是一直有个测试点过不了:

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

int n,la,lb;
string a,b;
int nextt[1100000];

void get_next(string s,int len)
{
    int j,k;
    j=0;k=-1;
    nextt[0]=-1;
    while(j<len)
    {
        if(k == -1||s[j] == s[k])
        {
            ++k;
            ++j;
            nextt[j]=k;
        }
        else
            k=nextt[k];
    }
}
int KMP(int pos)
{
   get_next(b,lb);
    int i,j;
    i=pos;//主串从pos处开始匹配
    j=0;
    while(i<la)//主串匹配完,此时子串的匹配长度就是我们要求的长度值,也就是j,所以返回j
    {
        if(j == -1 ||a[i] == b[j])
        {
            ++i;
            ++j;
        }
        else
            j=nextt[j];
    }
    return j;
}
int main()
{
    cin>>n>>a;
    la=a.size();
    n=n-1;
    while(n--)
    {
        cin>>b;
        lb=b.size();
        int pos=KMP(max(0,la-lb));//如果前串长则只需要从后串长度后缀处开始匹配,否则从0开始
        //cout<<"pos="<<pos<<endl;
        a+=b.substr(pos,lb);
        //cout<<a<<endl;
        la+=lb-pos;
    }
    cout<<a;
    return 0;
}

 把上面这个string类处改成char字符串数组就过了

(ac代码:)

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

int n,la,lb;
char a[1100000],b[1100000];
int nextt[1100000];

void get_next(char *s,int len)
{
    int j,k;
    j=0;k=-1;
    nextt[0]=-1;
    while(j<len)
    {
        if(k == -1||s[j] == s[k])
        {
            ++k;
            ++j;
            nextt[j]=k;
        }
        else
            k=nextt[k];
    }
}
int KMP(int pos)
{
   get_next(b,lb);
    int i,j;
    i=pos;//主串从pos处开始匹配
    j=0;
    while(i<la)//主串匹配完,此时子串的匹配长度就是我们要求的长度值,也就是j,所以返回j
    {
        if(j == -1 ||a[i] == b[j])
        {
            ++i;
            ++j;
        }
        else
            j=nextt[j];
    }
    return j;
}
int main()
{
    cin>>n>>a;
    la=strlen(a);
    n=n-1;
    while(n--)
    {
        cin>>b;
        lb=strlen(b);
        int pos=KMP(max(0,la-lb));//如果前串长则只需要从后串长度后缀处开始匹配,否则从0开始
        for(int j=pos;j<lb;j++)
            a[la++]=b[j];
    }
    cout<<a;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值