当前位置: 首页 > news >正文

杭州网站设计的公司网站建设项目推文

杭州网站设计的公司,网站建设项目推文,源码购买网站,建设工程质量管理条例网站题目描述 求采用顺序结构存储的串s和串t的一个最长公共子串,若没有则输出false,若最长的有多个则输出最先出现的那一串。 输入 输入两个字符串 输出 输出公共子串 样例输入 abcdef adbcef 样例输出 bc 分析:用字符串哈希解决。检查…
题目描述

求采用顺序结构存储的串s和串t的一个最长公共子串,若没有则输出false,若最长的有多个则输出最先出现的那一串。

输入

输入两个字符串

输出

输出公共子串

样例输入
abcdef
adbcef
样例输出
bc

分析:用字符串哈希解决。检查子串的哈希值是否相等,如果相等,说明是公共子串。书上的模板代码只能求出最长长度,添加一个下标位置即可。

#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include   <string>
#include   <vector>
#include   <cstdio>
#include    <queue>
#include    <stack>
#include    <ctime>
#include    <cmath>
#include      <map>
#include      <set>
#define INF 0x3fffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
using namespace std;typedef struct node
{int hashvalue,index;
}node;const long long mod=1e9+7;
const long long p=1e7+19;
const long long maxn=1010;
long long powp[maxn],h1[maxn],h2[maxn];
//vector<pair<int,int>>pr1,pr2;
vector<pair<node,int>>pr1,pr2;void init(int len)
{powp[0]=1;for(int i=1;i<=len;++i)powp[i]=powp[i-1]*p%mod;
}void calh(long long h[],string &str)
{h[0]=str[0];for(int i=1;i<str.length();++i)h[i]=(h[i-1]*p+str[i])%mod;
}int calsinglesubh(long long h[],int i,int j)
{if(i==0)return h[j];return ((h[j]-h[i-1]*powp[j-i+1])%mod+mod)%mod;
}void calsubh(long long h[],int len,vector<pair<node,int>>&pr)
{for(int i=0;i<len;++i){for(int j=0;j<len;++j){int hashvalue=calsinglesubh(h,i,j);
//            pr.push_back(make_pair(hashvalue,j-i+1));node temp;temp.hashvalue=hashvalue,temp.index=i;pr.push_back(make_pair(temp,j-i+1));}}
}int getmax()
{
//    int ans=0;int ans=0,index=-1;for(int i=0;i<pr1.size();++i){for(int j=0;j<pr2.size();++j){
//            if(pr1[i].first==pr2[j].first)
//                ans=max(ans,pr1[i].second);if(pr1[i].first.hashvalue==pr2[j].first.hashvalue){if(ans<pr1[i].second)ans=pr1[i].second,index=i;}}}
//    return ans;return index;
}int main(void)
{#ifdef testfreopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);clock_t start=clock();#endif //teststring str1,str2;getline(cin,str1);getline(cin,str2);init(max(str1.length(),str2.length()));calh(h1,str1);calh(h2,str2);calsubh(h1,str1.length(),pr1);calsubh(h2,str2.length(),pr2);
//    printf("ans=%d\n",getmax());int ind=getmax();if(ind==-1)printf("false\n");else{int index=pr1[ind].first.index,len=pr1[ind].second;for(int i=index,j=0;j<len;++i,++j)printf("%c",str1[i]);}#ifdef testclockid_t end=clock();double endtime=(double)(end-start)/CLOCKS_PER_SEC;printf("\n\n\n\n\n");cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位#endif //testreturn 0;
}

http://www.dtcms.com/a/550699.html

相关文章:

  • Nginx 主要的几种负载均衡模式
  • 网站宣传内容赣州大余做网站建设
  • 工信部怎么查网站备案西安市发布最新消息
  • 【18】C语言位运算符详解
  • 基于SpringBoot+Vue的中凯农场农资管理系统(权限分配、Echarts图形化分析)
  • 电子取证之windows知识点:从 0 到 1 掌握 Windows 事件日志取证:以 玄机靶场-第五章 Windows 实战-evtx 文件分析
  • UVa 11853 Paintball
  • 北京网站制作公司兴田德润在那里中国最新领导班子
  • 优秀网站seo报价wordpress亚马逊cdn
  • 韶关网站设计公司建设网站 请示 报告
  • 网站开发 兼职挣钱吗WordPress dux3.0
  • 建设网站和公告号的意义网上平台
  • 磁盘分区方案GPT和MBR的区别浅谈
  • 怎么用ps做网站首页字贵阳大数据论坛
  • php p2p网站源码网站icp备案申请流程
  • Java的Stream详解
  • 国家网站备案查询定制开发教程
  • MySQL安装及启用(社区版)
  • whois域名查询网站iis 添加网站 win7
  • 图像分割深度学习学习总结
  • 中铁建设集团网站能用VUE做网站
  • 网站超链接怎么做 word文档网页设计免费模板网站推荐
  • 网站支付接口怎么做百度站长怎么做网站维护
  • 查建筑材料的网站大数据系统
  • 建设银行 北京招聘网站网站代码 公告栏 php
  • leetcode3040.相同分数的最大操作数目II
  • dz网站标题公司做网站的费用用途写什么
  • 从局域网工具到全球传输:FastSend的无服务器共享革命
  • PostgreSQL 定位索引损坏位置
  • 做网站销售怎么找客户用群晖做网站服务器