一,字符串的简单介绍

例:POJ1488   http://poj.org/problem?id=1488

题意:替换文本中的双引号;

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std; int main()
{
char c,flag=1;
//freopen("Atext.in","r",stdin);
while((c=getchar())!=EOF){
if(c=='"'){printf("%s",(flag? "``" : "''"));flag=!flag;}
else printf("%c",c);
}
return 0;
}

二,模式匹配------Brute Force与KMP简介

1,Brute Force算法

例:POJ3080  http://poj.org/problem?id=3080 枚举,BF

新:strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。

Blue Jeans

Description

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 researcher, you have been tasked with writing a program that will find commonalities amongst given snippets of DNA that can be correlated with individual survey information to identify new genetic markers. 

A DNA base sequence is noted by listing the nitrogen bases in the order in which they are found in the molecule. There are four bases: adenine (A), thymine (T), guanine (G), and cytosine (C). A 6-base DNA sequence could be represented as TAGACC. 

Given a set of DNA base sequences, determine the longest series of bases that occurs in all of the sequences.

Input

Input to this problem will begin with a line containing a single integer n indicating the number of datasets. Each dataset consists of the following components:

  • A single positive integer m (2 <= m <= 10) indicating the number of base sequences in this dataset.
  • m lines each containing a single base sequence consisting of 60 bases.

Output

For each dataset in the input, output the longest base subsequence common to all of the given base sequences. If the longest common subsequence is less than three bases in length, display the string "no significant commonalities" instead. If multiple subsequences of the same longest length exist, output only the subsequence that comes first in alphabetical order.

Sample Input

3
2
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

Sample Output

no significant commonalities
AGATAC
CATCATCAT

Source

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std; int main()
{
//freopen("Atext.in","r",stdin);
int n,m,len;
char ans[70],s[15][65],tmp[65];
cin >> n;
while(n--){
cin >> m;
int k=3,flag=0; //枚举的字符串长度;
ans[0]='\0',tmp[0]='\0',len=0;
for(int i=0;i<m;i++)
for(int j=0;j<60;j++)
cin >> s[i][j];
while(k<=60){ //字符串起点
for(int i=0;i<=60-k;i++){ //枚举长度为k的字符串的起点
memset(tmp,0,sizeof(tmp));//必须记得清空数组!!
for(int j=i,t=0;j<i+k;j++)//这里的i+k,原来敲的是k,傻了傻了,还找了半天!!!
tmp[t++]=s[0][j];
for(int j=1;j<m;j++){
if(strstr(s[j],tmp)==NULL){flag=1;break;}//不是公共子串就标记跳出;
}
if(flag==0){
if(k>len){strcpy(ans,tmp);len=k;}
else if(k==len&&strcmp(ans,tmp)>0){strcpy(ans,tmp);len=k;}
}
flag=0;
}
k++;
}
if(len!=0){
for(int i=0;i<len;i++)
cout << ans[i] ;
}
else
cout << "no significant commonalities" ;
cout << endl;
}
return 0;
}

2,KMP算法

 例:POJ3461 Ouliop

 例:POJ3461 Oulipo

#include <iostream>
#include <cstdio>
#include <cstring>
const int maxn=10005;
using namespace std;
string s,t;
int n,m;
int nex[maxn];
void getnex(){
int j=0,k=-1;
nex[0]=-1;
while(j<n){
if(k==-1||t[j]==s[k]){
nex[++j]=++k;
}else
k=nex[k];
}
}
int kmp(){
int i=0,j=0,cnt=0;
getnex();
while(i<m){
if(j==-1||s[i]==t[j]){
i++;j++;
}else
j=nex[j];
if(j==n)
cnt++;
}
return cnt;
}
int main()
{
int c;
//freopen("Atext.in","r",stdin);
ios::sync_with_stdio(false); //加了这个,关闭了输入输出同步就过了,不然超时;
cin >> c;
while(c--){
int ans=0;
cin >> t >> s;
n=t.size();
m=s.size();
ans=kmp();
cout << ans << endl;
}
return 0;
}

字符串处理------Brute Force与KMP的更多相关文章

  1. 常用字符串匹配算法(brute force, kmp, sunday)

    1. 暴力解法 // 暴力求解 int Idx(string S, string T){ // 返回第一个匹配元素的位置,若没有匹配的子串,则返回-1 int S_size = S.length(); ...

  2. 数据结构(十六)模式匹配算法--Brute Force算法和KMP算法

    一.模式匹配 串的查找定位操作(也称为串的模式匹配操作)指的是在当前串(主串)中寻找子串(模式串)的过程.若在主串中找到了一个和模式串相同的子串,则查找成功:若在主串中找不到与模式串相同的子串,则查找 ...

  3. 字符串模式匹配算法--BF和KMP详解

    1,问题描述 字符串模式匹配:串的模式匹配 ,是求第一个字符串(模式串:str2)在第二个字符串(主串:str1)中的起始位置. 注意区分: 子串:要求连续   (如:abc 是abcdef的子串) ...

  4. DVWA全级别之Brute Force(暴力破解)

    Brute Force Brute Force,即暴力(破解),是指黑客利用密码字典,使用穷举法猜解出用户口令. 首先我们登录DVWA(admin,password),之后我们看网络是否为无代理,: ...

  5. DVWA实验之Brute Force(暴力破解)- High

    DVWA实验之Brute Force(暴力破解)- High   有关DVWA环境搭建的教程请参考: https://www.cnblogs.com/0yst3r-2046/p/10928380.ht ...

  6. DVWA实验之Brute Force(暴力破解)- Medium

    DVWA实验之Brute Force(暴力破解)- Medium   有关DVWA环境搭建的教程请参考: https://www.cnblogs.com/0yst3r-2046/p/10928380. ...

  7. DVWA Brute Force:暴力破解篇

    DVWA Brute Force:暴力破解篇 前言 暴力破解是破解用户名密码的常用手段,主要是利用信息搜集得到有用信息来构造有针对性的弱口令字典,对网站进行爆破,以获取到用户的账号信息,有可能利用其权 ...

  8. DVWA之Brute Force

    DVWA简介 DVWA(Damn Vulnerable Web Application)是一个用来进行安全脆弱性鉴定的PHP/MySQL Web应用,旨在为安全专业人员测试自己的专业技能和工具提供合法 ...

  9. DVWA(二): Brute Force(全等级暴力破解)

    tags: DVWA Brute Force Burp Suite Firefox windows2003 暴力破解基本利用密码字典使用穷举法对于所有的账号密码组合全排列猜解出正确的组合. LEVEL ...

  10. 小白日记46:kali渗透测试之Web渗透-SqlMap自动注入(四)-sqlmap参数详解- Enumeration,Brute force,UDF injection,File system,OS,Windows Registry,General,Miscellaneous

    sqlmap自动注入 Enumeration[数据枚举] --privileges -U username[CU 当前账号] -D dvwa -T users -C user --columns  [ ...

随机推荐

  1. Linux系统mysql免安装版配置指南

    1.下载(/usr/local目录) wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.32-linux-glibc2.12-x ...

  2. 虚拟环境 venv的使用--(venv 一)

    一.引言最近在学习<Flask Web Development 2nd Edition>的时候,作者开篇就介绍如何使用 venv 来管理 Python 环境 .自我觉得作者讲的非常好,想要 ...

  3. objectarx调用python注意点

    1. 用conda先装好虚拟环境,为了兼容cad2010,最高只能用python3.5(已经不维护了), 如果cad高版本建议python3.6+ 2. 能用pip安装的就用pip安装, 不能用pip ...

  4. Windows 任务栏透明 居中

    任务栏设置 任务栏居中 由于windows默认的程序是从左往右显示,看着可能有一点不舒服,当然如果使用习惯的话,忽然居中,可能更不舒服 首先搜索字符映射表 点击字符映设表中第五行的空白字符,选择进行复 ...

  5. 内存取证 volatility的使用

    volatility 简介: volatility(挖楼推了推) 是一个开源的框架,能够对导出的内存镜像进行分析,能够通过获取内核的数据结构,使用插件获取内存的详细情况和运行状态,同时可以直接dump ...

  6. 深入理解 Python 虚拟机:浮点数(float)的实现原理及源码剖析

    深入理解 Python 虚拟机:浮点数(float)的实现原理及源码剖析 在本篇文章当中主要分析在 cpython 虚拟机当中 float 类型的实现原理以及与他相关的一些源代码. Float 数据结 ...

  7. 一文明白:JavaScript异步编程

    同步和异步 JS是单线程 JavaScript语言的一大特点是单线程,同一时间只能做一件事 (单线程的JS 就是一个傻子,脑子一根筋,做着当前的这件事情,没有完成之前,绝对不会做下一件事情) 当然,这 ...

  8. Linux & 标准C语言学习 <DAY9_1>

        2.函数传参:         1.函数中定义的变量属于该函数,出了该函数就不能再被别的函数直接使用         2.实参与形参之间是以赋值的方式进行传递数据的,并且是单项值传递     ...

  9. KMP算法的研究

    前脚学后脚忘,是时候给自己通俗易懂的总结一下了 KMP是什么 在计算机科学中,Knuth-Morris-Pratt字符串查找算法(简称为KMP算法)可在一个字符串S内查找一个词W的出现位置.一个词在不 ...

  10. ACM-NEFUOJ-P239回文数

    #include<bits/stdc++.h> using namespace std; int n,p[1000],len,p1[1000]; int f() { int i; for( ...