校赛——1096Is The Same?(KMP或字符串的最小、大表示法)
1096: Is The Same?
Time Limit: 1 Sec Memory Limit: 64 MB
Submit: 26 Solved: 8
[Submit][Status][Web Board]
Description
Input
Output
Sample Input
2
abcd
bcda
abcd
bcad
Sample Output
Yes
No
比赛的时候用的是据说效率差不多的strstr过的,毕竟题目只是要求输出是否匹配而不是查找出现位置。KMP比较长打起来费时间而且清空数组又要打一堆
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
#define MM(a) memset(a,0,sizeof(a))
const int N=100010;
char a[N],b[N],aa[2*N],bb[2*N];
int nexta[N],nextb[N];
inline void getnext(int ne[],char s[])
{
int j=0,k=ne[0]=-1;
int len=strlen(s);
while (j<len)
{
if(k==-1||s[j]==s[k])
{
j++;
k++;
ne[j]=k;
}
else
k=ne[k];
}
}
inline bool kmp(int ne[],char s[],char p[])
{
int i=0,j=0;
int la=strlen(s),lb=strlen(p);
while (i<la&&j<lb)
{
if(s[i]==p[j]||j==-1)
{
i++;
j++;
}
else
j=ne[j];
}
if(j==lb)
return true;
else
return false;
}
int main(void)
{
int tcase,i,j;
scanf("%d",&tcase);
while (tcase--)
{
MM(a);
MM(b);
MM(aa);
MM(bb);
MM(nexta);
MM(nextb);
scanf("%s%s",a,b);
if(strlen(a)!=strlen(b))
{
puts("No");
continue;
}
strcat(aa,a);
strcat(aa,a);
strcat(bb,b);
strcat(bb,b);
getnext(nexta,a);
getnext(nextb,b);
if(kmp(nexta,bb,a)||kmp(nextb,aa,b))
puts("Yes");
else
puts("No");
}
return 0;
}
最小表示法代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
inline string minP(string s)
{
int i=0,j=1,k=0,l=s.size();
while (i<l&&j<l&&k<l)
{
int t=s[(i+k)%l]-s[(j+k)%l];
if(!k)
k++;
else
{
if(t>0)
j+=k+1;
else
i+=k+1;
k=0;
if(i==j)
j++;
}
}
s=s+s;
return s.substr(min(i,j),l);
}
int main(void)
{
int tcase;
string a,b;
cin>>tcase;
while (tcase--)
{
cin>>a>>b;
a=minP(a);
b=minP(b);
if(a==b)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}
校赛——1096Is The Same?(KMP或字符串的最小、大表示法)的更多相关文章
- 字符串的最小最大表示法O(n)
以下介绍内容内容转自:http://blog.csdn.net/zy691357966/article/details/39854359 网上看了这篇文章后还是感觉有些地方讲的没有详细的证明所以添加了 ...
- codeforces 825F F. String Compression dp+kmp找字符串的最小循环节
/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...
- Uvalive - 3026 Period (kmp求字符串的最小循环节+最大重复次数)
参考:http://www.cnblogs.com/jackge/archive/2013/01/05/2846006.html 总结一下,如果对于next数组中的 i, 符合 i % ( i - n ...
- 2019HDU多校赛第二场 H HDU 6598 Harmonious Army(最小割模型)
参考博客https://blog.csdn.net/u013534123/article/details/97142191 #include<bits/stdc++.h> using na ...
- 2019 HDU 多校赛第二场 HDU 6598 Harmonious Army 构造最小割模型
题意: 有n个士兵,你可以选择让它成为战士还是法师. 有m对关系,u和v 如果同时为战士那么你可以获得a的权值 如果同时为法师,你可以获得c的权值, 如果一个为战士一个是法师,你可以获得b的权值 问你 ...
- HDU 3374 最小/大表示法+KMP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题意:给定一个串s,该串有strlen(s)个循环同构串,要求输出字典序最小的同构串的下标,字典 ...
- kuangbin专题十六 KMP&&扩展KMP HDU3347 String Problem(最小最大表示法+kmp)
Give you a string with length N, you can generate N strings by left shifts. For example let consider ...
- HDU 3374 exkmp+字符串最大最小表示法
题意 找到一个字符串中最先出现的最小(大)表示位置,和最小(大)表示串出现次数 分析 用最小(大)表示法求出最先出现的最小(大)表示位置,然后将串长扩两倍用exkmp找出现次数. Code #incl ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
随机推荐
- ueditor1_3_6 一点问题记录
文件:getRemoteImage.php 第49行: if ( !in_array( $fileType , $config[ 'allowFiles' ] ) || stristr( $heads ...
- ctDNA|endosymbiosis
5.10叶绿体基因组编码多种蛋白质和RNA 叶绿体和线粒体的共同点:叶绿体和线粒体的大小,功能(编码区)大体一致,但叶绿体拥有更多基因,所以在编码tRNA时,也有内含子作为被剪切片段. 因为在原核生物 ...
- Shell脚本调用ftp上传文件
Shell脚本调用ftp上传文件 1.脚本如下 ftp -n<<! open x.x.x.x ###x.x.x.x为ftp地址 user username password ###user ...
- c++ json字符串转换成map管理
在cocos2dx for lua中,我们经常通过lua的table传入c++使用,然后早c++层操作数据. 实现步骤大致如下: table->string->c++层->通过rap ...
- 【最大流】bzoj1711: [Usaco2007 Open]Dining吃饭
正在网络流入门(原来这种题用网络流做) Description 农夫JOHN为牛们做了很好的食品,但是牛吃饭很挑食. 每一头牛只喜欢吃一些食品和饮料而别的一概不吃.虽然他不一定能把所有牛喂饱,他还是想 ...
- 重写laravel 异常抛出处理
所有异常错误都由类App\Exceptions\Handler处理,该类包含两个方法:report和render. 这里我们只看render方法,该方法会将异常渲染到HTTP响应中,就是说上面的错误信 ...
- redis+PHP消息队列实现及应用
学习视频: http://www.imooc.com/learn/852 学习笔记: https://blog.csdn.net/qq_33862644/article/details/7938564 ...
- COMP9021——6.3
有关yield的用法简介以及图灵机 第一节课大体没有太大变化,前半节课为了给图灵机的讲解做铺垫引入了yield.数组.字符串和文件等都是一个可迭代的对象,但由于它们的所有数据都存储与内存中,对内存的消 ...
- 设置vim 永久显示行号
永久显示行号:如果想让vim永久显示行号,则需要修改vim配置文件vimrc.如果没有此文件可以创建一个.在启动vim时,当前用户根目录下的vimrc文件会被自动读取,因此一般在当前用户的根目录下创建 ...
- pandas-Notes1
#coding = utf-8 import pandas as pd import numpy as np import matplotlib as plt # series, like vecto ...