POJ 3450 Corporate Identity(kmp求多个字符串的最长公共子串)

算法学习 - 最长公共子序列(LCS)C++实现 最长公共子序列最长公共子序列的问题很简单,就是在两个字符串中找到最长的子序列,这里明确两个含义: 子串:表示连续的一串字符 。 子序列:表示不连续的一串字符。 所以这里要查找的是不连续的最长子序列,动态规划这里为什么要使用动态规划可以说一下,简单来说动态规划是为了降低时间复杂度的一种算法,申请一个额外空间,来保存每一个步骤的结果,最后从这些结果中找到最优的解。这里有个问题就是: 阅读详情
http://poj.org/problem?id=3450
#include <stdio.h>
#include <string.h>

const int max_N=4001;
const int max_l=201;

int n;
char res[max_l],dict[max_N][max_l];
int nexts[max_N];

int min(int a,int b){return a<b?a:b;}
int max(int a,int b){return a>b?a:b;}

void get_nexts(char *s,int len)
{
    memset(nexts,0,sizeof(nexts));
    for(int i=1,j=0;i<len;)
    {
        if(s[i]==s[j])nexts[i++]=++j;
        else if(j>0) j=nexts[j-1];
        else i++;
    }
}

int get_longestpre(char *s,int len)
{
    get_nexts(s,len);
    for(int i=1;i<n;i++)
    {
        char *p=dict[i];
        int j=0,tmp=0;
        for(;*p&&j<len;)
        {
            if(*p==s[j])
            {
                p++,j++;
                tmp=max(tmp,j);
            }
            else if(j>0) j=nexts[j-1];
            else p++;
        }
        len=tmp;
    }
    return len;
}

int main()
{
    while(scanf("%d",&n)&&n)
    {
        getchar();
        for(int i=0;i<n;i++)
        {
            gets(dict[i]);
        }
        int len=strlen(dict[0]),ans=0,pos=0;

        for(int i=0;i<len;i++)
        {
            int tmp=get_longestpre(dict[0]+i,len-i);
            if(tmp>=ans)
            {
                if(tmp>ans)
                {
                    ans=tmp;
                    pos=i;
                }
                else
                {
                    bool smaller =true;
                    for(int t=0;t<ans;t++)
                    {
                        if(dict[0][pos+t]>dict[0][i+t]) break;
                        else if(dict[0][pos+t]<dict[0][i+t])
                        {
                            smaller=0;
                            break;
                        }
                    }
                    if(smaller)
                        pos=i;
                }
            }
        }
    if(ans)
    {
        for(int i=0;i<ans;i++)
            putchar(dict[0][pos+i]);

        printf("\n");
    }
    else
        puts("IDENTITY LOST");
    }
    return 0;
}

详情请见:http://www.cnblogs.com/mfrbuaa/p/5027323.html 这位大神的微博

KMP算法详解(与最长公共子序列) 一:在介绍KMP算法之前,先介绍一下BF算法 (1)BF算法(传统的匹配算法,也是最简单的算法)  BF算法是普通的模式匹配算法,BF算法的思想就是将目标串S的第一个字符与模式串P的第一个字符进行匹配,若相等,则继续比较S的第二个字符和P的第二个字符;若不相等,则比较S的第二个字符和P的第一个字符,依次比较下去,直到得出最后的匹配结果。     (2)举例说明:     S:  abab 阅读详情

相关推荐

camera 知识速查

本文总结了Camera开发中的常见问题排查方法和关键技术点。主要内容包括:I2C/MIPI不通的排查步骤、时序问题分析方法、Sensor/AF/Flash/PDAF的Bring Up流程、OTP调试方法、PDAF原理及调试、CTS/ITS问题解决思路、AE震荡解决方案、Systrace性能分析方法、Flicker产生原理、Tombstone问题定位等。还涉及V4L2框架解析、问题定位分层方法、Sensor Setting注意事项、时钟信号概念、第三方应用卡顿排查、大光圈概念、Monkey测试方法。

qq_41665131的博客 787

KMP:最长连续公共子序列

大佬博客: 请点这里 题意:         给你n个字符串,要你求出这n个字符串的最长公共连续子序列是哪个,如果存在多个最长的,就输出字典序最小的那个。 &lt;span style="font-size:18px;"&gt;#include &lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;algorithm&gt; #inclu...

qq_40859951的博客 773

避免bank conflict的使用共享内存的矩阵转置。及其循环展开。转置前后都是行主序,中间共享内存中是列主序

为了避免bank conflict,可以使用padding的方式将数组中的每一行各自占据一个bank,这样每个线程读取不同行的数据时就不会发生bank conflict。padding的方式是在每行数据后面添加一定数量的空数据,使得每行的字节数恰好是bank的整数倍,这样就能保证每行数据各自占据一个bank。具体来说,当一个线程访问共享内存中的一个元素时,GPU会根据该元素在共享内存中的地址计算出其所在的bank,然后将该元素从该bank中读取出来。

weixin_55035144的博客 2153

KMP算法快速理解

一、最长公共子缀 公共子缀分为前缀和后缀 前缀:总是包含第一个字符的子串(不包括父串本身) 后缀:总是包含最后一个字符的子串(不包括父串本身) ———————————————————————————— 举个例子: 父串ABCDEAB 前缀:A、AB、ABC、ABCD、ABCDE、ABCDEA 后缀:B、AB、EAB、DEAB、CDEAB、BCDEAB 最长公共子缀就是指完全相同的最长前缀和后缀 在...

Aaron_Mars的博客 682

微带天线理论学习笔记(1)

微带天线的概念是由Deschamps于1953年提出来的,它是在一块厚度远小于工作波长的介质基片的一面敷以金属辐射片、一面全部敷以金属薄层作接地板而成。辐射片可以根据不同的要求设计成各种形状。微带天线具有质量轻、体积小和易于制造等优点,广泛应用于个人无线通信中。

qq_36521926的博客 6241

RC4加密算法详解

RC4(也称为 Rivest Cipher 4)是流密码的一种形式,流密码逐字节操作数据流。RC4 流密码是使用最广泛的流密码之一,它通过算法一次一个字节地加密消息,简单并且操作速度快。RC4是一种在电子信息领域加密的技术手段,用于无线通信网络,是一种电子密码,只有经过授权的用户才能享受该服务,使用 64 位或 128 位密钥大小。它通常用于安全套接层 (SSL)、传输层安全 (TLS) 等应用,也用于 IEEE 802.11 无线 LAN 标准。在存储大量的信息时,RC4展现出它的实用性,面对大量的数据,

weixin_43211186的博客 1万+

poj3080多个字符串找最长公共子串 kmp

http://poj.org/problem?id=3080 kmp是改进版的暴力字符串匹配,明确两个概念——子串和子序列,子串就必须是连续的,子序列不一定,dp有一个问题是最长公共子序列,这里求的是公共子串。 kmp算法最主要的就是一个next数组,一个kmp匹配函数。 这道题目的意思就是输入不超过十个字符串,每个字符串的长度不超过60,然后找出最长的公共子串,如果有好几个子串长度相同,就

Gloria971111的博客 1782

nRF5340双核实战:从Zephyr环境搭建到蓝牙协议栈部署

本文详细介绍了nRF5340双核处理器的开发实践,包括Zephyr环境搭建、蓝牙协议栈部署及双核协同工作配置。通过实战案例展示了如何利用nRF5340的双核异构架构提升蓝牙应用性能,并提供了调试技巧与资源优化建议,帮助开发者高效实现低功耗蓝牙设备开发。

weixin_42525385的博客 390

POJ 3450 Corporate Identity

POJ 3450 Corporate Identity Description Beside other services, ACM helps companies to clearly state their “corporate identity”, which includes company logo but also other signs, like trademarks. One o...

旺崽的博客 2480

1、VMware vSphere 全面指南:从入门到精通

本文全面介绍了VMware vSphere从入门到精通的完整知识体系,涵盖基础概念、环境适配、部署规划、安装配置、网络与存储准备、虚拟机创建与管理、资源组织、事件监控、巡检流程、主机配置文件管理、资源池优化、高可用性与容错应用,以及故障排除方法。结合实用工具和部署技巧,帮助读者系统掌握vSphere的核心功能与运维实践,提升虚拟化环境的稳定性与管理效率。

pz890123的博客 100

【OpenClaw从入门到精通】命令行操作指南——TUI模式高效管理OpenClaw(2026避坑版)

本文将详细介绍OpenClaw的命令行操作,重点介绍TUI(Text User Interface)模式的高效使用方法,以及在实际使用中需要避免的常见陷阱。通过本文的学习,你将掌握OpenClaw命令行的精髓,提升工作效率。

一键难忘的博客 1709

POJ 3450 Corporate Identity 求所有字符的最长公共子串(kmp)

Corporate Identity Time Limit:3000MS Memory Limit:65536K Total Submissions:9460 Accepted:3122 Description Beside other services, ACM helps companies to clearly state their...

qq_43813140的博客 381

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

[KMP或者暴力]POJ 3450 Corporate Identity

传送门:http://poj.org/problem?id=3450 题目大意:前面那道题类似,求多个字符串的最长且字典序最小的公共子串,还是枚举子串,然后拿去和剩余主串匹配,保存最优解。 代码: #include #include #include using namespace std; const int MAXN = 222,MAXM = 4444; char s[MAXM][MAX

Airarts_的专栏 890

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

poj 3450 Corporate Identity

http://poj.org/problem?id=3450 Corporate Identity Description Beside other services, ACM helps companies to clearly state their “corporate identity”, which includes company logo but also ot

大太阳 372

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

【poj 3450 Corporate Identity 】 KMP(暴力)

C - Corporate Identity Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u SubmitStatusDescriptionBeside other services, ACM helps companies to clearly state their “corporate

ATOD 1060

POJ-3450 Corporate Identity

枚举一个串的所有子串,和其余的串匹配,裸kmp的题。 #include #include #include using namespace std; const int maxn = 222; const int maxd = 4010; int f[maxn]; char str[maxn]; char t_str[maxn]; char c_str[maxd][maxn]; void ini

花月阁 752

poj_3450_Corporate Identity(kmp)

Corporate Identity http://poj.org/problem?id=3450 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9929 Accepted: 3275 Description Beside other services, ACM helps companies to clearly sta...

qq_43816599的博客 220

【扩展KMP】POJ_3450| HDU_2328 Corporate Identity

原题直通车: 题意概述:

PWZER 630

poj 3450 Corporate Identity (KMP+最长公共子串)

题目链接:   poj 3450 题目大意:   给定n个字符串,找出最长的公共子串,若长度小于3则输出IDENTITY LOST 解题思路:   选出长度最短的字符串,枚举它的子串                   把它的子串分别与其余的n-1个字符串匹配                   字符串长度越短它的子串就越少,枚举量越少                   枚举子串的顺序

ンZee的解题报告 1489

poj3450 Corporate Identity

题意:给你若干个字符串,求它们的最长公共子串。对于相同长度的最长公共子串,取字典序最小的那个。 思路:枚举第一个字符串的所有可能子串,对后面所有字符串进行匹配。 #include #include #include using namespace std; const int maxn=1001; bool kmp(string a,string b) { int i,

gemini小窝 420

POJ3450 Corporate Identity 【KMP 暴力strstr可过】

题目描述 给你n个字符串,问其最长的公共串是啥。如果长度都是最长,输出字典序小的。 Sample Input 3 aabbaabb abbababb bbbbbabb 2 xyz abc 0 Sample Output abb IDENTITY LOST 解题思路 取第一个字符串O(n^2)遍历,对其他字符串直接strstr匹配即可。 用K

Bill_Utada的成长之路 667

POJ - 3450 - Corporate Identity

Corporate Identity Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 7268   Accepted: 2546 Description Beside other services, ACM helps companies to clearly st

lose__way的博客 303

POJ - 3450Corporate Identity(暴力kmp)

https://vjudge.net/contest/189927#problem/F 题意:几个串,问你这几个串中的所有字串的最长公共长度。 思路: 暴力枚举第一个字串的所有子串然后和后面的所有串暴力匹配,复杂度??不存在的,比较坑的点是要字典序最小。 上代码把: #include #include #include #include using namespace st

wjmwsgj的博客 257

POJ 3450 Corporate Identity 求所有字符的最长公共子串

Description 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 Inter

靖空间 1668
上一篇: POJ 2774 Long Long Message (后缀数组求最长公共子串)
下一篇: POJ 3450--->Corporate Identity(后缀数组求多个字符串的公共子串)
My_stage
博客等级 码龄11年 15粉丝 158原创
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值