HDUOJ------1711Number Sequence
Number Sequence
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9114 Accepted Submission(s): 4166
/*@kmp扩展@龚细军*/
#include<stdio.h>
#include<string.h>
int aa[],bb[];
int next[];
//依旧使用next数组
void get_next(int *pt,int len)
{
memset(next,,sizeof(next));
int i=,j=-;
next[]=-;
while(i<len)
{
if(j==-||pt[i]==pt[j])
{
++i;
++j;
if(pt[i]!=pt[j])
next[i]=j;
else
next[i]=next[j];
}
else
j=next[j];
}
}
//kmp的扩展
int exd_kmp(int *ps,int *pt,int lens,int lent)
{
int i=-,j=-;
get_next(pt,lent);
while(i<lens)
{
if(j==-||ps[i]==pt[j])
{
++i;
++j;
}
else
j=next[j];
if(j==lent)break;
}
if(j==lent)
return i-j+;
else
return -;
} int main()
{
int test,n,m,i;
scanf("%d",&test);
while(test--)
{
scanf("%d%d",&n,&m);
for(i=;i<n;i++)
scanf("%d",&aa[i]);
for(i=;i<m;i++)
scanf("%d",&bb[i]);
printf("%d\n",exd_kmp(aa,bb,n,m));
}
return ;
}
java代码:
import java.util.Scanner;
public class Main {
public static void main(String args [])
{
mt aa = new mt();
Scanner reader =new Scanner(System.in);
int test=reader.nextInt();
while((test--)>0)
{
aa.lena=reader.nextInt();
aa.lenb=reader.nextInt();
aa.init(aa.lena+1, aa.lenb+1);
for(int i=0;i<aa.lena;i++)
aa.a[i]=reader.nextInt();
for(int i=0;i<aa.lenb;i++)
aa.b[i]=reader.nextInt();
kmp(aa);
}
}
private static void kmp(mt aa)
{
int i,j;
aa.next[i=0]=-1;
j=-1;
while(i<aa.lenb)
{
if(j==-1||aa.b[i]==aa.b[j])
{
i++;
j++;
if(aa.b[i]==aa.b[j])
aa.next[i]=aa.next[j];
else
aa.next[i]=j;
}
else
j=aa.next[j];
}
i=j=0;
while(i<aa.lena&&j<aa.lenb)
{
if(j==-1||aa.a[i]==aa.b[j])
{
i++;
j++;
}
else j=aa.next[j];
}
if(j==aa.lenb)
out(i-aa.lenb+1);
else
out(-1);
}
private static void out(int aa)
{
System.out.println(aa);
}
}
class mt
{
int [] b,a,next;
int lena,lenb;
void init(int lena,int lenb)
{
a=new int [lena];
b=new int [lenb];
next =new int [lenb];
}
}
HDUOJ------1711Number Sequence的更多相关文章
- hduoj 2062Subset sequence
Subset sequence Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDUOJ Number Sequence 题目1005
/*Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- hdu 1711Number Sequence (KMP入门,子串第一次出现的位置)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 1711Number Sequence
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 数字KMP,原来还能这么用 #include<stdio.h> ],b[]; ]; ...
- HDU 1711Number Sequence【KMP模板题】
<题目链接> 题目大意: 意思是给出两个串,找出匹配串在模式串中的位置. 解题分析: KMP算法模板题. #include <cstdio> #include <cstr ...
- oracle SEQUENCE 创建, 修改,删除
oracle创建序列化: CREATE SEQUENCE seq_itv_collection INCREMENT BY 1 -- 每次加几个 STA ...
- Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等
功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...
- DG gap sequence修复一例
环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Sequence Reconstruction 序列重建
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
随机推荐
- struts2中<welcome-file>index.action</welcome-file>直接设置action,404的解决方案
这几天的项目页面的访问全部改为.action访问,在修改首页时遇到了问题.将web.xml文件中<welcome-file>index.action</welcome-file> ...
- dlib landmark+人面识别
#include "stdafx.h" #include <dlib/image_processing/frontal_face_detector.h> #includ ...
- (转载)ios的一些知识点
ios的一些知识点 一 非ARC的内存管理情况 1-autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一 段落 ...
- SVN使用小结
SVN是Subversion的简称.是一个开放源码的版本号控制系统.在它的管理下,文件和文件夹能够超越时空的限制,权且当作一种奇妙的"时间机器"吧. 基本功能 版本号控制 作为一个 ...
- scala编程第16章学习笔记(4)——List对象的方法
通过元素创建列表:List.apply List(1, 2, 3) 等价于List.apply(1, 2, 3): scala> List.apply(1, 2, 3) res0: List[I ...
- 把Scala代码当作脚本运行
1. 在类UNIX系统上作为脚本运行 在类Unix系统上,你可以设置一个shell前导词来执行脚本.如下例: Script.scala #!/usr/bin/env scala !# println( ...
- GO语言基础map与函数
1. map 1. 类似其它语言中的哈希表活着字典,以 key-value 形式存储数据 2. key 必须是支持 == 或 != 比较运算的类型,不可以是函数.map 或 slice 3. map ...
- [Algorithm] Trie data structure
For example we have an array of words: [car, done, try, cat, trie, do] What is the best data structu ...
- C语言:strcpy()和memcpy()
一.strcpy和memcpy都是标准C库函数,它们有下面的特点: 1.strcpy提供了字符串的复制.即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制字符串的结束符. ...
- Cocos2d-x 3.0多线程异步资源载入代码
// AppDelegate.cpp bool AppDelegate::applicationDidFinishLaunching() { - - FlashScene* scene = Flash ...