POJ3450题解——暴力orKMP

题目链接:http://poj.org/problem?id=3450

Corporate Identity

Beside other services, ACM helps companies to clearly state their “corporate identity”, which includes company logo but also other signs, like trademarks. One of such companies is Internet Building Masters (IBM), which has recently asked ACM for a help with their new identity. IBM do not want to change their existing logos and trademarks completely, because their customers are used to the old ones. Therefore, ACM will only change existing trademarks instead of creating new ones.

After several other proposals, it was decided to take all existing trademarks and find the longest common sequence of letters that is contained in all of them. This sequence will be graphically emphasized to form a new logo. Then, the old trademarks may still be used while showing the new identity.

Your task is to find such a sequence.

Input

The input contains several tasks. Each task begins with a line containing a positive integer N, the number of trademarks (2 ≤ N ≤ 4000). The number is followed by N lines, each containing one trademark. Trademarks will be composed only from lowercase letters, the length of each trademark will be at least 1 and at most 200 characters.

After the last trademark, the next task begins. The last task is followed by a line containing zero.

Output

For each task, output a single line containing the longest string contained as a substring in all trademarks. If there are several strings of the same length, print the one that is lexicographically smallest. If there is no such non-empty string, output the words “IDENTITY LOST” instead.

Sample Input

3
aabbaabb
abbababb
bbbbbabb
2
xyz
abc
0

Sample Output

abb
IDENTITY LOST

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x3f3f3f3f      //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]= {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]= {-1, 1, 0, 0, -1, 1, -1, 1};

int t,ansl,next[210];
string s[4010],p,ans;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(cin>>t&&t)
    {
        ansl=0;
        for(int i=0;i<t;i++)cin>>s[i];
        for(int i=0;i<s[0].size();i++)
        {
            int l=s[0].size()-i;
            p=s[0].substr(i,l);

            next[0]=-1;
            int slen=-1;
            int plen=0;
            while(plen<l)
            {
                if(slen==-1||p[plen]==p[slen])
                {
                    plen++;slen++;
                    if(p[plen]!=p[slen])
                    next[plen]=slen;
                    else
                    next[plen]=next[slen];
                }
                else slen=next[slen];
            }

            int snk,max=210;
            for(int j=1;j<t;j++)
            {
                plen=0;slen=0;snk=0;
                while(plen<l&&slen<s[j].size())
                {
                    if(plen==-1||p[plen]==s[j][slen])
                    {
                        plen++;slen++;
                    }
                    else plen=next[plen];
                    if(plen>snk)
                    {
                        snk=plen;
                    }
                }
                if(snk<max)max=snk;
            }

            if(max>ansl)
            {
                ansl=max;
                ans=s[0].substr(i,max);
                ans[ansl]=‘\0‘;
            }
            else if(ansl==max)
            {
                string temp;
                temp=s[0].substr(i,max);temp[max]=‘\0‘;
                if(temp<ans)
                {
                    ans=temp;ans[ansl]=‘\0‘;
                }
            }
        }
        if(ansl)cout<<ans<<endl;
        else cout<<"IDENTITY LOST"<<endl;
    }
    return 0;
}

KMP

原文地址:https://www.cnblogs.com/Mingusu/p/11809864.html

时间: 2024-10-14 01:18:34

POJ3450题解——暴力orKMP的相关文章

POJ3080题解——暴力orKMP

题目链接:http://poj.org/problem?id=3080 The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated. As an IBM resea

【bzoj题解】1012 最大数

题目描述 现在请求你维护一个数列,要求提供以下两种操作:1.查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度.2.插入操作.语法:A n 功能:将n加上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取模,将所得答案插入到数列的末尾.限制:n是非负整数并且在长整范围内.注意:初始时数列是空的,没有一个数. 输入 第一行两个整数,M和D,其中M表示操作的个数(M <= 200,0

【bzoj4052】[Cerc2013]Magical GCD 暴力

题目描述 给出一个长度在 100 000 以内的正整数序列,大小不超过 10^12. 求一个连续子序列,使得在所有的连续子序列中,它们的GCD值乘以它们的长度最大. 样例输入 1 5 30 60 20 20 20 样例输出 80 题解 暴力 由于$\gcd$具有结合律,所以如果$\gcd(a,b)$比$a$小,那么至少小了一半. 所以所有以一个数为右端点的区间中,本质不同的$\gcd$个数只有$\log a$个. 于是从左向右枚举右端点,统计出以该点为右端点的所有$\gcd$以及区间长度,统计答

【bzoj2793】[Poi2012]Vouchers 暴力

题目描述 考虑正整数集合,现在有n组人依次来取数,假设第i组来了x人,他们每个取的数一定是x的倍数,并且是还剩下的最小的x个.正整数中有m个数被标成了幸运数,问有哪些人取到了幸运数. 输入 第一行一个正整数m (m<=1,000,000),下面m行每行一个正整数x (x<=1,000,000),表示x是一个幸运数.接下来一行一个正整数n (n<=1,000,000),下面n行每行一个正整数x (x<=1,000,000),表示这一组来了x个人. 输出 第一行输出一个非负整数k,表示

【bzoj1495】[NOI2006]网络收费 暴力+树形背包dp

题目描述 给出一个有 $2^n$ 个叶子节点的完全二叉树.每个叶子节点可以选择黑白两种颜色. 对于每个非叶子节点左子树中的叶子节点 $i$ 和右子树中的叶子节点 $j$ :如果 $i$ 和 $j$ 的颜色都为当前节点子树中颜色较多(相等视为白色)的那个,则不需要付出代价:都为较小的那个则需要付 $2f[i][j]$ 的代价:否则需要付 $f[i][j]$ . 求最小代价. 输入 输入文件中第一行有一个正整数N. 第二行有2N个整数,依次表示1号,2号,…,2N号用户注册时的付费方式,每一个数字若

【题解】黑白奶牛

题目描述 有N只奶牛从左往右排成一行,编号是1至N.这N只奶牛当中,有一些奶牛是黑色的,其余的是白色的. color[i]表示第i只奶牛的颜色,如果color[i]=0则表示第i头奶牛是黑色的,如果color[i]=1则表示第i头奶牛是白色的. 六一奶牛儿童节快到了,农场主Farmer John要从这N头奶牛当中,挑选尽可能多的奶牛去参加晚会. Farmer John挑选奶牛的原则是:挑选编号是连续的一段奶牛,这一段奶牛的颜色必须全部是白色的. Farmer John有一个魔法棒,每用一次魔法棒

HDU 6697 Closest Pair of Segments (计算几何 暴力)

2019 杭电多校 10 1007 题目链接:HDU 6697 比赛链接:2019 Multi-University Training Contest 10 Problem Description The closest pair of points problem is a well-known problem of computational geometry. In this problem, you are given \(n\) points in the Euclidean plan

[BZOJ1552][Cerc2007]robotic sort

试题描述 输入 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000.第二行为N个用空格隔开的正整数,表示N个物品最初排列的编号. 输出 输出共一行,N个用空格隔开的正整数P1,P2,P3-Pn,Pi表示第i次操作前第i小的物品所在的位置. 注意:如果第i次操作前,第i小的物品己经在正确的位置Pi上,我们将区间[Pi,Pi]反转(单个物品). 输入示例 6 3 4 5 1 6 2 输出示例 4 6 4 5 6 6 数据规模及约定 见"输入" 题解 暴力

[NOIp 2016]愤怒的小鸟

Description Input Output Sample Input 22 01.00 3.003.00 3.005 21.00 5.002.00 8.003.00 9.004.00 8.005.00 5.00 Sample Output 11 Sample Explanation HINT 题解 暴力做法: 1.因为三点可以确定一条抛物线,又必过原点,那么只需要再找两个点就能确定一条抛物线; 2.枚举点对,求出抛物线方程,注意两点的$x$坐标不能相等,抛物线的二次项系数必须小于$0$;