CodeForces - 357D - Xenia and Hamming
先上题目:
1 second
256 megabytes
standard input
standard output
Xenia is an amateur programmer. Today on the IT lesson she learned about the Hamming distance.
The Hamming distance between two strings s = s1s2... sn and t = t1t2... tn of equal length n is value . Record [si ≠ ti] is the Iverson notation and represents the following: if si ≠ ti, it is one, otherwise — zero.
Now Xenia wants to calculate the Hamming distance between two long strings a and b. The first string a is the concatenation of n copies of string x, that is, . The second string b is the concatenation of m copies of string y.
Help Xenia, calculate the required Hamming distance, given n, x, m, y.
The first line contains two integers n and m (1 ≤ n, m ≤ 1012). The second line contains a non-empty string x. The third line contains a non-empty string y. Both strings consist of at most 106 lowercase English letters.
It is guaranteed that strings a and b that you obtain from the input have the same length.
Print a single integer — the required Hamming distance.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.
100 10
a
aaaaaaaaaa
0
1 1
abacaba
abzczzz
4
2 3
rzr
az
5
In the first test case string a is the same as string b and equals 100 letters a. As both strings are equal, the Hamming distance between them is zero.
In the second test case strings a and b differ in their 3-rd, 5-th, 6-th and 7-th characters. Thus, the Hamming distance equals 4.
In the third test case string a is rzrrzr and string b is azazaz. The strings differ in all characters apart for the second one, the Hamming distance between them equals 5.
题意:给出两个串的循环节以及循环次数,求这两个串的汉明距离,这里的汉明距离指的是对应位置的字符如果不一样就是加1。
这里的数据有点大,循环节的长度就有10^6,循环次数最大10^12,所以不能直接暴搜。这里的做法是先求出两个循环节的最小公倍数l和最大公约数g。然后统计面每个字符串某个位置i是哪个字符,同时保存在i%g的位置,因为在一个l的范围里面只有在g的倍数的位置两个字符串的字符才会有比较,然后统计一下l的范围里面两个字符串每一个位置的不同的字符的对数。然后因为最大公约数l的距离以后字符串的异同又会和前面一样,所以我们只要再乘以一个字符串的总长度对于l的倍数就可以了。
但是这里需要优化,因为比较同一个位置的字符异同的时候比较次数是26*26-26如果再乘上g的话有可能会非常大,这样就会超时,所以我们需要考虑它的反面,我们可以用一个字符串的总长度剪去位置上有相同字符的数量,这样我们就可以减少一维变成g*26了。
但是这样做可能还是会wa,因为我们还需要 注意到数据大小(10^6)*(10^12)快要到达long long 的极限了,如果我们是先用前面的到的结果ans先乘上这里的数再做后面的除法的话会有溢出的可能,所以我们需要先求了字符串的总长度对于l的倍数,然后再将ans乘以倍数,这样就不会爆long long了。
上代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 1000002
#define ll long long
using namespace std; char a[MAX],b[MAX];
ll n,m,r,le;
ll la,lb,g,l,ans;
int dpa[MAX][];
int dpb[MAX][]; ll gcd(ll a,ll b){
return b== ? a : gcd(b,a%b);
} int main()
{
//freopen("data.txt","r",stdin);
while(scanf("%I64d %I64d",&n,&m)!=EOF){
scanf("%s",a);
scanf("%s",b);
la=strlen(a);
lb=strlen(b);
g = a>b ? gcd(la,lb) : gcd(lb,la);
l=la*lb/g;
memset(dpa,,sizeof(dpa));
memset(dpb,,sizeof(dpb));
for(int i=;i<la;i++){
dpa[i%g][a[i]-'a']++;
}
for(int i=;i<lb;i++){
dpb[i%g][b[i]-'a']++;
}
ans=;
for(int i=;i<g;i++){
for(int j=;j<;j++){
ans+=(ll)dpa[i][j]*(ll)dpb[i][j];
}
}
ans=n*la/l*ans;
ans=n*la-ans;
printf("%I64d\n",ans);
}
return ;
}
/*357D*/
CodeForces - 357D - Xenia and Hamming的更多相关文章
- Codeforces Round #207 (Div. 1) B. Xenia and Hamming(gcd的运用)
题目链接: B. Xenia and Hamming 题意: 要求找到复制后的两个字符串中不同样的字符 思路: 子问题: 在两串长度是最大公倍数的情况下, 求出一个串在还有一个串中反复字符的个数 CO ...
- codeforces B. Xenia and Spies 解题报告
题目链接:http://codeforces.com/problemset/problem/342/B 题目意思:有n个spy,编号从1-n,从左到右排列.现在的任务是,spy s要把信息传递到spy ...
- codeforces A. Xenia and Divisors 解题报告
题目链接:http://codeforces.com/problemset/problem/342/A 题目意思:给出n个数,找出n/3个组且每组有3个数,这三个数必须要符合两个条件:1.a < ...
- codeforces B. Xenia and Ringroad 解题报告
题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...
- cf D. Xenia and Hamming
http://codeforces.com/contest/357/problem/D 题意:给你两个数n和m,表示两个字符串的循环次数,然后给出两个字符串,求出其相同位置字符不同的个数. 先求出两个 ...
- codeforces 339C Xenia and Bit Operations(线段树水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Xenia and Bit Operations Xenia the beginn ...
- codeforces 339C Xenia and Weights(dp或暴搜)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Xenia and Weights Xenia has a set of weig ...
- codeforces 342D Xenia and Dominoes(状压dp+容斥)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud D. Xenia and Dominoes Xenia likes puzzles ...
- [Codeforces 339D] Xenia and Bit Operations
[题目链接] https://codeforces.com/problemset/problem/339/D [算法] 线段树模拟即可 时间复杂度 :O(MN) [代码] #include<bi ...
随机推荐
- 57.部门职位管理 ExtJs 展示
1.jobInfo.jsp <%@ page language="java" pageEncoding="UTF-8"%> <script t ...
- [Swift通天遁地]二、表格表单-(16)在表单行内嵌入日期和时间拾取器
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [Swift通天遁地]五、高级扩展-(4)快速生成Invert、Mix、Tint、Shade颜色及调整饱和度阶
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [Swift通天遁地]八、媒体与动画-(3)实现视频播放的水印、Overlay、暂停时插入广告等效果
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- glances内存分析工具使用
glances -b 以字节为单位显示网络流量 glances 是一个命令行工具包括如下命令选项:-b:显示网络连接速度 Byte/ 秒-B @IP|host :绑定服务器端 IP 地址或者主机名称- ...
- JavaScript--编程练习1
使用JS完成一个简单的计算器功能.实现2个输入框中输入整数后,点击第三个输入框能给出2个整数的加减乘除. 提示:获取元素的值设置和获取方法为:例:赋值:document.getElementById( ...
- 329 Longest Increasing Path in a Matrix 矩阵中的最长递增路径
Given an integer matrix, find the length of the longest increasing path.From each cell, you can eith ...
- Log4J2的 PatternLayout
Log4J2 PatternLayout 参考 日志样例 : 2018-10-21 07:30:05,184 INFO - DeviceChannelServiceImpl.java:434[defa ...
- 使用 Spring Social 连接社交网络
Spring Social 框架是spring 提供社交平台的分享组件 https://www.ibm.com/developerworks/cn/java/j-lo-spring-social/
- 【sqli-labs】 less53 GET -Blind based -Order By Clause -String -Stacked injection(GET型基于盲注的字符型Order By从句堆叠注入)
http://192.168.136.128/sqli-labs-master/Less-53/?sort=1';insert into users(id,username,password) val ...