题目:POJ3080 http://poj.org/problem?id=3080

题意:对于输入的文本串,输出最长的公共子串,如果长度相同,输出字典序最小的。

这题数据量很小,用暴力也是16ms,用后缀数组可以到0ms,但我不会XD。

暴力:

 #include<cstdio>
#include<cstring>
using namespace std;
char str[][],ans[];
int main(){
int T,n; scanf("%d", &T);
while (T--){
memset(ans,,sizeof(ans));
scanf("%d", &n);
for (int i = ; i < n; i++) scanf("%s", str[i]); for (int i = ; i < strlen(str[]); i++)
for (int j = i + ; j < strlen(str[]); j++){
char s[];//子串
strncpy(s, str[] + i, j - i + );///strncpy
s[j - i + ] = '\0'; int flag = ;
for (int k = ; flag && k < n; k++)
if (strstr(str[k], s) == NULL)
flag = ;
///匹配成功,判长度和字典序
if (flag && (j - i + > strlen(ans) || (j - i + == strlen(ans)&&strcmp(ans, s)>) ) )
strcpy(ans, s);
} if (strlen(ans) < ) puts("no significant commonalities");
else puts(ans);
}
return ;
}

KMP:

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char str[][],ans[];
int next[];
//计算串str的next数组
void getnext(char *str){
int len=strlen(str);
int j=,k=-;
next[]=-;
while(j<len){
if(k==-||str[j]==str[k]) next[++j]=++k;
else k=next[k];
}
} //返回串S中第一次出现串T的开始位置
int KMP(char *S,char *T){
int l1=strlen(S), l2=strlen(T);
int i=,j=;
while(i<l1){
if(j==-||S[i]==T[j])
i++, j++;
else j=next[j];
if(j==l2) return i-l2+;
}
return -;//若一直不匹配则返回-1
} int main(){
int T,n;
scanf("%d", &T);
while(T--){
memset(ans,,sizeof(ans));
scanf("%d", &n);
for(int i=;i<n;i++) scanf("%s", str[i]); for(int i=; i<strlen(str[]); i++)//把第一个串的每一个子串当作模板串,求next数组,并和后面的每一个串去匹配
for(int j=i+; j<strlen(str[]); j++){
char s[];
strncpy(s, str[] + i, j - i + );///strncpy
s[j - i + ] = '\0';
getnext(s); int flag = ;
for(int k = ; flag&&k < n; k++)
if(KMP(str[k], s)==-)
flag = ;
///匹配成功,判长度和字典序
if(flag&& ( strlen(s)>strlen(ans) || ( strlen(s)==strlen(ans)&&strcmp(s, ans)<)))
strcpy(ans, s);
} if(strlen(ans) < ) puts("no significant commonalities");
else puts(ans);
}
return ;
}

题目:POJ3461 http://poj.org/problem?id=3461

题意:求第一个串在第二个串中出现的次数

 //求第一个串在第二个串中出现的次数
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define MAXN 1000050
char s[MAXN],t[MAXN];
int next[MAXN];
void getnext(char *str){
int len=strlen(str);
int j=,k=-;
next[]=-;
while(j<len){
if(k==-||str[j]==str[k]) next[++j]=++k;
else k=next[k];
}
} int KMP(char *S,char *T){//返回S中出现T的次数
int l1=strlen(S), l2=strlen(T);
int ans=,i=,j=;
while(i<l1){
if(j==-||S[i]==T[j])
i++, j++;
else j=next[j];
if(j==l2) ans++;
}
return ans;
}
int main(){
int T; scanf("%d",&T);
while(T--){
scanf("%s%s",t,s);
getnext(t);//获得第一个串的next数组去匹配第二个串
printf("%d\n",KMP(s,t));
}
return ;
}

POJ 3080 Blue Jeans、POJ 3461 Oulipo——KMP应用的更多相关文章

  1. poj 3080 Blue Jeans (暴力枚举子串+kmp)

    Description The Genographic Project is a research partnership between IBM and The National Geographi ...

  2. POJ 3080 Blue Jeans (求最长公共字符串)

    POJ 3080 Blue Jeans (求最长公共字符串) Description The Genographic Project is a research partnership between ...

  3. POJ 3080 Blue Jeans 找最长公共子串(暴力模拟+KMP匹配)

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20966   Accepted: 9279 Descr ...

  4. poj 3080 Blue Jeans

    点击打开链接 Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10243   Accepted: 434 ...

  5. POJ 3080 Blue Jeans (字符串处理暴力枚举)

    Blue Jeans  Time Limit: 1000MS        Memory Limit: 65536K Total Submissions: 21078        Accepted: ...

  6. POJ 3080 Blue Jeans(Java暴力)

    Blue Jeans [题目链接]Blue Jeans [题目类型]Java暴力 &题意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 规定: 1. 最长公共 ...

  7. POJ 3080 Blue Jeans (KMP)

    求出公共子序列  要求最长  字典序最小 枚举第一串的所有子串   然后对每一个串做KMP.找到目标子串 学会了   strncpy函数的使用   我已可入灵魂 #include <iostre ...

  8. POJ - 3080 Blue Jeans 【KMP+暴力】(最大公共字串)

    <题目链接> 题目大意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 限制条件: 1.  最长公共串长度小于3输出   no significant co ...

  9. poj 3080 Blue Jeans 解题报告

    题目链接:http://poj.org/problem?id=3080 该题属于字符串处理中的串模式匹配问题.题目要求我们:给出一个DNA碱基序列,输出最长的相同的碱基子序列.(保证在所有的序列中都有 ...

随机推荐

  1. bzoj 4318 || 洛谷P1654 OSU!

    https://www.lydsy.com/JudgeOnline/problem.php?id=4318 https://www.luogu.org/problemnew/show/P1654 看来 ...

  2. asp.net core分块上传文件

    写完asp.net多文件上传(http://www.cnblogs.com/bestckk/p/5987383.html)后,感觉这种上传还是有很多缺陷,于是...(省略一万字,不废话).这里我没用传 ...

  3. 转 shell模拟数据库的读写

    0.create table create table myTestTable as select rownum as id,               to_char(sysdate + rown ...

  4. [在读]JavaScript异步编程:设计快速响应的网络应用

    很棒的一本,就如书名所示,主要讲js异步的一些东西,比如定时器.Jquery的promise和deffered,node...看了一小半.推荐哦~~

  5. wireshark工具集

    tshark 查看pcap文件第一个包的时间,当文件名不包含时间信息时非常有帮助 tshark -c 1 -T fields -e frame.time -r test.pcap dumpcap ed ...

  6. CSS3基础知识学习

    CSS3动画例子展示 http://www.17sucai.com/pins/demoshow/13948 HTML5和CSS3特效展示 http://www.html5tricks.com/30-m ...

  7. Java编程简介

    作者:CHAITANYA SINGH 来源:https://www.koofun.com//pro/kfpostsdetail?kfpostsid=3 JAVA由Sun Microsystems In ...

  8. IDEA JavaSE环境配置

    需指定JDK路径: File -> Project Structure -> Project -> Project SDK -> New -> 选择JDK所在的根目录

  9. [转]Fedora 下安装NVIDIA显卡驱动(使用后无法进入图形界面)

    http://www.linuxidc.com/Linux/2014-04/100689.htm rpmfusion安装法: 相对于ATi,在Linux下安装NVIDIA就简单得多.只需要一个命令即可 ...

  10. 打开一个本地apk进行安装

    Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); File file = new File(Environment ...