Oulipo HDU - 1686(哈希或KMP)匹配字符串

题意:字符串匹配:寻找字符串S中,字符串T出现的次数

思路:KMP或哈希

The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book: 

Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais… 

Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive 'T's is not unusual. And they never use spaces. 

So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {'A', 'B', 'C', …, 'Z'} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap. 
 

Input

The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format: 

One line with the word W, a string over {'A', 'B', 'C', …, 'Z'}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W). 
One line with the text T, a string over {'A', 'B', 'C', …, 'Z'}, with |W| ≤ |T| ≤ 1,000,000.

Output

For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T. 
 

Sample Input

3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN

Sample Output

1
3
0

代码:KMP

/*next数组的含义就是一个固定字符串的最长前缀和最长后缀相同的长度。
ABCEFGHRHHABCABCEFGHRHHABC
末尾(ABC)能跟开头(ABC)重合的最大长度就是前缀和子缀的最大公共长度*/
#include<stdio.h>
#include<string.h>
using namespace std;
int n,a,b,dp[10010];
char u[10010],v[1000010];
void ne()//**注意最长前缀:是说以第一个字符开始,但是不包含最后一个字符。*/
{int i=0,j=-1;dp[0]=-1;//next[0]初始化为-1,-1表示不存在相同的最大前缀和最大后缀while(i<a){if(j==-1||u[i]==u[j]) dp[++i]=++j;//这个是把算的k的值(就是相同的最大前缀和最大后缀长)赋给next[q]else j=dp[j];//如果下一个不同,那么k就变成next[k],注意next[k]是小于k的,无论k取任何值。}//往前回溯
}
void KMP()
{int ans=0;int i=0,j=0;while(i<a&&j<b){if(i==-1||u[i]==v[j])j++,i++;else i=dp[i];if(i==a){ans++;i=dp[i];}}printf("%d\n",ans);
}
int main()
{scanf("%d",&n);while(n--){scanf("%s%s",u,v);a=strlen(u);b=strlen(v);ne();KMP();}return 0;
}

哈希

#include<stdio.h>/*哈希*/
#include<string.h>
#include<algorithm>
using namespace std;
typedef unsigned long long ull;
const int M=1e4+10;
const int p=13331;/*哈希的基数*/
int t,a,b;
char u[M],v[100*M];
ull dp[M];
ull h[M*100];
ull k;
void init()
{dp[0]=1;for(int i=1;i<M;i++)dp[i]=dp[i-1]*p;
}
ull geth(char *s)
{ull h=0;int len=strlen(s);for(int i=0;i<len;i++)h=h*p+s[i];return h;
}
void gethash(char *s)
{int len=strlen(s);h[0]=0;for(int i=0;i<len;i++)h[i+1]=h[i]*p+s[i];
}int main()
{scanf("%d",&t);init();while(t--){scanf("%s%s",u+1,v+1);k=geth(u+1);gethash(v+1);a=strlen(u+1);b=strlen(v+1);int ans=0;for(int i=0;i+a<=b;i++){ull tmp=h[i+a]-h[i]*dp[a];if(tmp==k)ans++;}printf("%d\n",ans);}return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/310047.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

我整理了100G的.Net学习资料,速来领取!

听说免费送课会上瘾&#xff1f;不分享点干货给大家学习&#xff0c;完全无法衬托本号主的好人特质啊&#xff01;正所谓白嫖一时爽&#xff0c;一直白嫖一直爽&#xff01;举起你滴双手&#xff0c;擦亮你的眼睛&#xff01;今天我要丢个硬核干货&#xff01;一次性怒砸几百个…

maven mysql的jdbctemplate_JDBC、JDBCTemplate、MyBatis、Hiberante 比较与分析

JDBC (Java Data Base Connection,java数据库连接)JDBC(Java Data Base Connection,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成.JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发…

[C++STL]list容器用法介绍

代码如下: #include <iostream> #include <string> #include <list> using namespace std;void printList(const list<int>&L) {for (list<int>::const_iterator it L.begin(); it ! L.end(); it){cout << *it << " &quo…

C#黔驴技巧之去重(Distinct)

关于C#中默认的Distinct方法在什么情况下才能去重&#xff0c;这个就不用我再多讲&#xff0c;针对集合对象去重默认实现将不再满足&#xff0c;于是乎我们需要自定义实现来解决这个问题&#xff0c;接下来我们详细讲解几种常见去重方案&#xff0c;孰好孰歹自行判之。分组首先…

Almost Union-Find UVA - 11987(并查集的删除操作)

题意&#xff1a;求出每个集合的元素个数&#xff0c;及总和&#xff0c;给出三个操作&#xff1a; 1 将含有a元素和b元素的集合合并&#xff1b;2 将a元素放入含有b元素的集合中&#xff1b;3 输出a元素所在集合的元素个数及总和&#xff1b; 思路&#xff1a;正常并查集&am…

java.sql 拒绝连接_hive jdbc 拒绝连接问题

本帖最后由 willgone 于 2017-06-21 10:56 编辑平台配置在附件中。private static String driverName1 "org.apache.hive.jdbc.HiveDriver";Class.forName(driverName1);String url"jdbc:hive2://192.168.160.241:21076/default";Connection con DriverM…

ASP.NET Core分布式项目实战(oauth2 + oidc 实现 server部分)--学习笔记

任务15&#xff1a;oauth2 oidc 实现 server部分基于之前快速入门的项目&#xff08;MvcCookieAuthSample&#xff09;&#xff1a;ASP.NET Core快速入门&#xff08;第5章&#xff1a;认证与授权&#xff09;--学习笔记ASP.NET Core快速入门&#xff08;第6章&#xff1a;ASP…

[C++STL]set容器用法介绍

代码如下: #include <iostream> #include <set> using namespace std;void printSet(set<int>&s) {for (set<int>::iterator it s.begin(); it ! s.end(); it){cout << *it << " ";}cout << endl; }void test01() {…

Equations HDU - 1496(哈希或三层for循环)求满足公式有多少种情况

题意:求x在(-100<x<100)区间上&#xff0c;已知a,b,c,d&#xff0c;满足a*x1^2b*x2^2c*x3^2d*x4^20 的情况有多少种 思路&#xff1a;很明显四层for循环肯定超时&#xff0c;可用三层for循环来判断&#xff0c;或是&#xff0c;两两组合&#xff0c;求多少种情况。分正负…

java.time.format例子_java格式化时间示例

实现日期的格式化&#xff0c;需要用到类: java.text.DateFormatDateFormat没有可以直接使用的构造函数&#xff0c;一般使用DateFormate的子类---java.text.SimpleDateFormat完成构造.public SimpleDateFormat(String pattern)测试代码import java.text.DateFormat;import jav…

[C++STL]map容器用法介绍

代码如下: #include <iostream> #include <string> #include <map> using namespace std;void printMap(map<int,int>&m) {for (map<int, int>::iterator it m.begin(); it ! m.end(); it){cout << "key " << it-&…

ABP框架 v2.7.0已经发布!

ABP框架和ABP商业版 v2.7已经发布.我们没有为2.4,2.5和2.6发布博客文章,所以这篇文章也将涵盖这几个版本中新增内容和过去的2个月里我们完成了什么.关于发布周期与开发之前说过我们已经开始每两个星期发布一个新的次要功能版本,一般在星期四.我们的目标是尽快提供新功能.在过去…

Colossal Fibonacci Numbers! UVA - 11582(斐波那契求模)+快速幂+周期规律

题意&#xff1a; 给出64位整数a、b以及不超过1000的正整数n&#xff0c;求斐波那契数列第a ^ b项模n的结果。 输入&#xff1a;情况数T&#xff0c;之后T行每行a、b、n。 输出&#xff1a;斐波那契数列第a ^ b项模n的结果。 分析&#xff1a;由于斐波那契数列的每一项都是由…

java中factory方法_Java的23中设计模式--工厂方法模式(Factory Method)

1.普通工厂模式工厂类/*** Title Factory.java* Package factory.factory1* date 2015-1-22 上午10:16:02*versionV1.0*/packagefactory.factory1;/*** ClassName Factory* date 2015-1-22 上午10:16:02*/public classFactory {publicSender procedure(String type) {if("…

GCD and LCM HDU - 4497(素数打表+唯一分解定理)求多少种情况

题目大意&#xff1a; 给你两个数最小公倍数L,最大公约数G&#xff0c;问你有多少有序数组&#xff08;x,y,z)满足GCD&#xff08;x,y,z)G,LCM(x,y,z)L,首先如果gcd(x,y,z)G, 思路分析&#xff1a; 当这样的组合存在的时候&#xff0c;求gcd&#xff08;x,y,z&#xff09;1且l…

[C++STL]仿函数用法介绍

代码如下: #include <iostream> #include <string> using namespace std;//函数对象在使用时&#xff0c;可以像普通函数那样调用&#xff0c;可以有参数&#xff0c;可以有返回值 class MyAdd { public:int operator()(int a, int b){return a b;} };void test0…

【壹刊】Azure AD(二)调用受Microsoft 标识平台保护的 ASP.NET Core Web API (上)

—————————Grant_Allen 是一位博客园新晋博主&#xff0c;目前开始专注于Azure方向的学习和研究&#xff0c;是我认识不多的、打算长时间研究Azure的群友&#xff0c;因此打算帮他开个专栏&#xff0c;同时也希望并祝愿他能一直坚持下去&#xff0c;学有所成。正文一&a…

[C++STL]常用遍历算法

代码如下&#xff1a; #include <iostream> #include <algorithm> #include <vector> using namespace std;void print01(int val) {cout << val << " "; }class print02 { public:void operator()(int val){cout << val <&…

用Visual Studio2019自定义项目模板

项目模板简介众所周知&#xff0c;在我们使用VS新建项目时&#xff0c;都需要选择一个项目模板&#xff0c;如下图&#xff1a;我们选择完项目模板进行创建&#xff0c;创建完成之后&#xff0c;可以发现项目中已经包含了一些基础的文件。例如MVC&#xff1a;可以看到&#xff…

A/B HDU - 1576 (逆元或拓展欧几里得或数学公式)多解法求大数结果

题意&#xff1a;求(A/B)%9973&#xff0c;但由于A很大&#xff0c;我们只给出n(nA%9973)(我们给定的A必能被B整除&#xff0c;且gcd(B,9973) 1)。 思维&#xff1a;&#xff08;1&#xff09;逆元扩展欧几里得算法&#xff1a;满足a*k≡1 (mod p)的k值就是a关于p的乘法逆元。…