String Shifting
我们规定对一个字符串的shift操作如下:略去。shift(string, x) = string(0 <= x < n).
分析:一看这题,这不很简单么,直接模拟判断,但是这套路有这么简单么!看数据范围,1e6,那么复杂度就是1e6*1e6=1e12,这样,在1s的时限内,肯定会超时,暴力肯定不行,那就字符串哈希,很早就想写了,但是没有遇到专门的这类题,刚好这道题就练习一下,我忘了那个算法叫什么名字,好像是Rabin-Karp,反正就是对这个字符串算出一个值,然后可以O(1)的转移到下一个字符串,计算出下一个字符串的值,如果这两个值不相等,那么这两个字符串肯定不同,如果相等,那相同的概率就很高,这里需要再次逐个字符判断一下是否相等。这道题,我为了简单起见,就没有再次判断,已经ac。
/*
ID: y1197771
PROG: test
LANG: C++
*/
#include<bits/stdc++.h>
#define pb push_back
#define FOR(i, n) for (int i = 0; i < (int)n; ++i)
#define dbg(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl
typedef unsigned long long ll;
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e3 + ;
const int key = ;
int work(char x) {
if(x >= 'A' && x <= 'Z')
return x - 'A';
return + x - 'a';
}
void solve() {
string s, d;
cin >> s;
d = s;
int res = ;
int n = s.size();
ll t = , tag = , tar = ;
for (int i = ; i < n; i++) {
t = t * key + work(s[i]);
tag *= key;
}
tar = t;
for (int i = ; i < n - ; i++) {
t = t * key - tag * work(s[i]) + work(s[i]);
if(t == tar) res++;
}
cout << res << endl; }
int main() {
freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
solve();
return ;
}
String Shifting的更多相关文章
- 剑指Offer——完美+今日头条笔试题+知识点总结
剑指Offer--完美+今日头条笔试题+知识点总结 情景回顾 时间:2016.9.28 16:00-18:00 19:00-21:00 地点:山东省网络环境智能计算技术重点实验室 事件:完美世界笔试 ...
- 哈希表(散列表)—Hash表解决地址冲突 C语言实现
哈希表(Hash table,也叫散列表),是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度.具体的介绍网上有很详 ...
- 哈希表的C实现(三)---传说中的暴雪版
关于哈希表C实现,写了两篇学习笔记,不过似乎网上流传最具传奇色彩的莫过于暴雪公司的魔兽文件打包管理器里的hashTable的实现了:在冲突方面的处理方面,采用线性探测再散列.在添加和查找过程中进行了三 ...
- AtCoder Beginner Contest 223
AtCoder Beginner Contest 223 A是纯纯的水题,就不说了 B - String Shifting 思路分析 我真的sb,一开始想了好久是不是和全排列有关,然后读了好几遍题目也 ...
- Symbols of String Pattern Matching
Symbols of String Pattern Matching in Introduction to Algorithms. As it's important to be clear when ...
- Educational Codeforces Round 1 B. Queries on a String 暴力
B. Queries on a String Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/59 ...
- 科普:String hashCode 方法为什么选择数字31作为乘子
1. 背景 某天,我在写代码的时候,无意中点开了 String hashCode 方法.然后大致看了一下 hashCode 的实现,发现并不是很复杂.但是我从源码中发现了一个奇怪的数字,也就是本文的主 ...
- [LeetCode] Shifting Letters 漂移字母
We have a string S of lowercase letters, and an integer array shifts. Call the shift of a letter, th ...
- [Swift]LeetCode767. 重构字符串 | Reorganize String
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
随机推荐
- iOS 分类和继承
iOS 中分类(Categories) 和 继承(Inherit)有相同的功能,但在一些细节上又有差异,简单介绍一下两者的异同. 分类可以在不知道系统类源代码的情况下,为这个类添加新的方法.分类只能用 ...
- 深入C语言可变参数(va_arg,va_list,va_start,va_end,_INTSIZEOF)
一.什么是可变参数 在C语言编程中有时会遇到一些参数个数可变的函数,例如printf(),scanf()函数,其函数原型为: int printf(const char* format,…),int ...
- 解决Please ensure that adb is correctly located at 'D:\java\sdk\platform-tools\adb.exe' and can be executed.
遇到问题描述: 运行android程序控制台输出 [2012-07-18 16:18:26 - ] The connection to adb is down, and a severe error ...
- air写文件 SecurityError: fileWriteResource 时报错的解决方法
用 File.applicationDerectoryPath.resolv("text.txt")会报SecuriyError错误! 解决: var _Path:File = F ...
- careercup-中等难度 17.6
17.6 给定一个整数数组,编写一个函数,找出索引m和n,只要将m和n之间的元素排好序,整个数组就是有序的.注意:n越小越好,也就是说,找出符合条件的最短序列. 解法: 开始解题之前,让我们先确认一下 ...
- devenv.exe - Assert Failure visual studio 2010
安装完成devexpress后启动vs提示 devenv.exe - assert failure 第一种在博客园找到的解决方案: 原因是机器上安装了framework 4.5 和4.5中文包,卸载 ...
- java_jdbc_可滚动结果集与分页
public static void create2(int i) { Connection conn = null; Statement st = null; ResultSet rs = null ...
- A+B Problem III
描述 求A+B是否与C相等. 输入 T组测试数据. 每组数据中有三个实数A,B,C(-10000.0<=A,B<=10000.0,-20000.0<=C<=20000.0) 数 ...
- 按字母顺序排序的 arcpy.mapping 类列表
arcpy.mapping 类可使用地图文档 (.mxd) 或图层文件 (.lyr) 中的不同对象类型的各种方法和属性.此文档可专门用作快速参考.有关详细信息,请使用链接跳转至各帮助页面. arcpy ...
- mapping 详解3(Meta-Fields)
文档标识相关元数据字段 _index 当执行多索引查询时,可能需要添加特定的一些与文档有关联的索引的子句. _index 字段可以用在 term.terms 查询,聚合(aggregations)操作 ...