BUPT OJ 147. Substring
来源:互联网 发布:中科大苏州软件学院 编辑:程序博客网 时间:2024/06/11 02:33
题目描述
There're two strings S and T formed by English letters (lowercase and uppercase). You are required to find a longest substring R from string S, that string R can be generated only by string T (overlap is essential).
For example:
T = "AB" cannot generate "ABC" because T does not have character 'C';
T = "ABA" can generate "ABABA" and "ABABABA" but not "ABABAB";
T = "AB" cannot generate "ABAB".
输入格式
There are several test cases.
The first line of input contains an integer N which indicates the number of test cases. Each test case is formed by two lines. The first describes the string S, and the other describes T.
The length of each string will not exceed
输出格式
For each test case, you should output the longest length of string R.
输入样例
3ABCABCABCABABABABAABABAABA
输出样例
35
S菊苣之KMP解法
其实我想的是HASH
#include<bits/stdc++.h>using namespace std;vector<int> find_substr(string pattern, string text){ int n=pattern.size(); vector<int> next(n+1,0); for(int i=1; i<n; ++i){ int j=i; while(j>0){ j=next[j]; if(pattern[j]==pattern[i]){ next[i+1]=j+1; break; } } } vector<int> positions; int m=text.size(); for(int i=0, j=0; i<m; i++){ if(j<n&&text[i]==pattern[j]){ j++; } else{ while(j>0){ j=next[j]; if(text[i]==pattern[j]){ j++; break; } } } if(j==n){ positions.push_back(i-n+1); } } return positions;}const int MAXN=1000005;char a[MAXN], b[MAXN];string A, B;vector<int> v;deque<pair<int,int> > q;int main(){ int t; cin>>t; while(t--){ scanf("%s%s",a,b); A=string(a); B=string(b); v=find_substr(B,A); int n=B.size(), m=A.size(); if(v.size()==0){ puts("0"); continue; } int ans=0; while(!q.empty()) q.pop_back(); for(int i=0; i<v.size(); i++){ while(!q.empty()&&q.front().second+n<=v[i]){ q.pop_front(); } int ret; if(!q.empty()) ret=q.back().first-(q.back().second-v[i]); else ret=n; ans=max(ret,ans); q.push_back(make_pair(ret,v[i])); } printf("%d\n",ans); } return 0;}
- BUPT OJ 147. Substring
- BUPT OJ 文件系统
- BUPT OJ 矩阵幂
- BUPT OJ IP地址
- BUPT OJ 寻找宝藏
- BUPT OJ 虚数
- BUPT OJ 统计节点个数
- BUPT OJ 最小距离查询
- BUPT OJ 非平方等式
- BUPT OJ 96 矩阵幂
- BUPT OJ 101 IP地址
- BUPT OJ 中序遍历序列
- BUPT OJ 中序遍历树
- BUPT OJ 丁神又去谷歌
- bupt
- 南邮 OJ 1125 Substring
- 福大oj 2075 Substring
- Light OJ Substring Frequency (II)
- android中的sqlite数据库
- Java JDBC和MySQL
- virtualBox虚拟机的一些配置
- 并查集入门
- ORACLE_基础十二(Profiles)
- BUPT OJ 147. Substring
- poj 3616 Milking Time DP
- a new client --- using httpclient or curl or requests
- hibernate基于hql的基本查询
- 未文档化函数ObReferenceObjectByName的说明
- ajax无刷新实时验证用户名密码
- 实现的局域网内大文件传输(ftp功能)
- The Center of Gravity hdu2105
- Linux下多线程查看工具(pstree、ps、pstack)