HDU 2203 循环串匹配
来源:互联网 发布:汤灿到底怎么了知乎 编辑:程序博客网 时间:2024/06/16 15:12
模板串复制一遍那就行了
memmove(text + lenT, text, sizeof(char)*lenT);lenT *= 2;
然后就直接一样的模板
ac code
/*#include <bits/stdc++.h>#include <ext/pb_ds/priority_queue.hpp>#include <tr1/unordered_map>#include <ctime>using std::tr1::unordered_map;*/#include <iostream>#include <cstring>#include <string>#include <queue>#include <algorithm>#include <cstdio>#include <map>using namespace std;/* using std::sort; using std::bitset; using std::max; using std::cout; using std::stack; using std::cin; using std::endl; using std::swap; using std::pair; using std::vector; using std::set; using std::map; using std::multiset; using std::queue; using std::greater; using std::string; using std::priority_queue; using std::max_element; using std::min_element; using __gnu_pbds::pairing_heap_tag; __gnu_pbds::priority_queue<int, greater<int>, pairing_heap_tag> heap;#define Hash unordered_map*/#define pr(x) cout<<#x<<" = "<<x<<" "#define prln(x) cout<<#x<<" = "<<x<<endl#define lson o*2, L, M#define rson o*2+1, M + 1,R#define MAXN 1000010//pattern为模板串,从0下标,长度为len。 返回next数组template<typename T>void kmp_pre(T pattern[], int len, int next[]){next[0] = next[1] = 0;for(int i = 1 ; i < len ; i++){ int j = next[i];while(j && pattern[i] != pattern[j])j = next[j];next[i+1] = pattern[i] == pattern[j] ? j+1 : 0;}}//text为匹配串,lenT为其长度。pattern为模板串,lenP为其长度,next为上面得到的next数组。//返回一个vector,表示所有匹配成功的在text的下标(从0开始)//还返回一个true/false表示是否有匹配成功的template<typename T>bool find(T text[], int lenT, T pattern[], int lenP, int next[], vector<int> &ret)//下标皆为从0开始{ ret.clear();int j = 0; //初始化在模式串第一个位置for (int i = 0; i < lenT; ++ i){while (j && pattern[j] != text[i]) j = next[j];if (pattern[j] == text[i]) j++;if (j == lenP){j=0; //得到一个答案后,进行第二次匹配,j归0 ret.push_back(i-lenP+1);}}return ret.size();}vector<int>ans;int nxt[1000010];char pattern[1000010], text[1000010];int get_repeat(int idx, int nex[])//返回在idx位置,如果是重复串,重复了多少次。如果不重复,返回-k,表示还需要添加k个字符才能重复{int lenP = idx + 1;int cir = lenP - nxt[lenP];if (cir == lenP) return 1; //重复了一次,表明整个串前后没有相同部分else if (lenP % cir == 0) return lenP / cir; //返回idx结尾,重复的次数else {//表明不重复,返回最少要添加多少个字符,才能重复。此值为负数 return lenP % cir -cir;}}int main(){int T = 0;while (scanf("%s%s", text, pattern) == 2){int lenT = strlen(text);int lenP = strlen(pattern);memmove(text + lenT, text, sizeof(char)*lenT);lenT *= 2;//cout<<text<<" "<<pattern<<" "<<lenT<<endl;if (lenT<lenP){cout<<"no"<<endl;continue;}kmp_pre(pattern, lenP, nxt);find(text, lenT, pattern, lenP, nxt, ans);cout<< ( ans.size() ? "yes" : "no")<<endl;}return 0;}
0 0
- HDU 2203 循环串匹配
- 【hdu 2203】 亲和串 KMP匹配
- HDU 3746 字符串匹配(字符串的最小循环节问题)
- HDU 2203 亲和串(KMP:循环移位)
- HDU 2203 kmp简单匹配
- HDU 2896 AC自动机 模式串匹配
- HDU 1083 最大匹配
- hdu 1150 二分匹配
- hdu 1281 二分匹配
- hdu 3081 二分匹配
- HDU 1045 二分匹配
- HDU 1083 二分匹配
- HDU 1083 Courses 匹配
- hdu 2819 二分匹配
- hdu 1281 (最大匹配)
- hdu 1845 (最大匹配)
- hdu 1507 (最大匹配)
- hdu 1507 二分匹配
- LeetCode | Sudoku Solver
- 使用C语言操作Sqlite数据库
- Lucas+阶乘打表+费马小定理模板
- 从网页抓取城市间的距离
- Runtime学习与使用(一):为UITextField添加类目实现被键盘遮住后视图上移
- HDU 2203 循环串匹配
- js判断手机,邮箱格式是否正确
- event.getX()、event.getY()与event.getRawX()、event.getRawY()的区别
- mac安装mongodb
- Accessed None error from node
- Android 如何在进入有scrollView的页面时滑动条置顶
- 使用Java API创建(create),查看(describe),列举(list),删除(delete)Kafka主题(Topic)
- iOS多线程的初步研究(八)-- dispatch队列
- Android ContentProvider学习