【CodeForces - 357D】Xenia and Hamming (字符串问题,数论,思维)

题干:

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.

Input

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.

Output

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 %I64d specifier.

Examples

Input

100 10
a
aaaaaaaaaa

Output

0

Input

1 1
abacaba
abzczzz

Output

4

Input

2 3
rzr
az

Output

5

Note

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.

题目大意:

给出一个n,m 然后给出两个串的循环节S和T(不一定等长), n代表第一个串有多少次循环,m代表第二个串有多少次循环,问两个串的Hamming distance是多少,这个距离是指对应位置的字符如果相等对结果的贡献就是0,否者就是1。(加长的两个字符串等长)

解题报告:

   这是一道关于汉明距离的题目。首先涨一波知识:

这题第一眼应该就跟数论gcd,lcm相关(因为循环节和循环次数嘛最后还等长),而且数据量就知道不可能让你暴力,肯定是有重复计算,我们需要算那个最小的然后扩大相应的倍数。

于是首先想到只要比较了两个串的lcm长度,再扩大相应倍数就可以,但是这样的数据量还是会TLE,想想也知道啊,如果len1和len2互质的话,复杂度O(len1*len2),最坏为10^12。

想办法优化:

     分析一下会发现,在上面的想法中,有一部分比较是没有必要的,比如:

                  S:  abacdcde                 T:  acdaed

    S的长度为8,T的长度为6,len1与len2的gcd = 2,lcm = 24。

    比较时                

       

    T开头的a并不是和S中的每一位都能进行比较的,也就是说,在一个循环结中,可以一次性将T中的每一位与其将会与S中所对应的位置的字符都比较一下,也就是预处理出来。也不难发现,T的第一个字符,总与s[1]+k*gcd比较,第二个字符,总与s[2]+k*gcd比较...以此类推。(假设从s[1]为第一个字符而不是s[0])

设g=gcd(len1,len2),设ans为S和T中对应位置相同的字符的数目,方法就是在i from 0 to gcd(len1,len2)-1 的循环中,将S分为g个小段,每段长度为len1/g,统计每次该小段上第i个字符出现的次数,然后将T中g个小段的第i个字符出现的次数加入到ans中。最后将ans扩大相应倍数,然后再用总的长度减去ans就是答案。

传送门

AC代码1:(这是一个网络的代码,但是因为dpa数组开成了longlong,所以MLE了,下面有一个优化版本)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int qq = 1e6+10;
char a[qq],b[qq];
int dpa[qq][27],dpb[qq][27];
ll gcd(ll a, ll b){return b==0?a:gcd(b, a%b);
}
ll lcm(ll a, ll b){return a/gcd(a, b)*b;
}
int main(){ll n,m;scanf("%lld%lld",&n,&m);scanf("%s%s",a,b);ll lena = strlen(a);ll lenb = strlen(b);ll g = gcd(lena, lenb);	 ll l = lena/g*lenb;memset(dpa, 0, sizeof(dpa));memset(dpb, 0, sizeof(dpb));for(int i=0; i<lena; ++i)	dpa[i%g][a[i]-'a']++;for(int i=0; i<lenb; ++i)	dpb[i%g][b[i]-'a']++;ll ans = 0;for(int i=0; i<g; ++i)for(int j=0; j<26; ++j)ans+=(ll)dpa[i][j]*dpb[i][j];ans = n*lena/l*ans;	//每一个长度为l的字符串中,所含相同的字符有ans个、 // 求长度为n*lena/l 个长度为l的字符串中,所含相同字符的总数、 ans = n*lena - ans;	//求反面、 然后用总长度减去相同字符的个数、 printf("%lld\n", ans);return 0;
}

AC代码2:(这样不需要二维数组,时间慢了一丢丢200+ms,但是剩下了很多空间复杂度)

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX = 1e6 + 5;
char s1[MAX],s2[MAX];
ll cnt[50]; 
ll Gcd(ll a,ll b) {while(a^=b^=a^=b%=a);return b;
}int main()
{ll n,m;cin>>n>>m;cin>>s1>>s2;ll len1 = strlen(s1);ll len2 = strlen(s2);ll gcd = Gcd(len1,len2);ll lcm = len1 * (len2 / gcd);ll l1=len1/gcd,l2=len2/gcd;ll ans = 0;for(ll i = 0; i<gcd; i++) {memset(cnt,0,sizeof cnt);for(ll j = 0; j<l1; j++) {cnt[s1[gcd*j + i]-'a'] ++;}for(ll j = 0; j<l2; j++) {ans += cnt[s2[gcd*j+i]-'a'];}}ans *=len1*n/ lcm ;
//	ans *= (m*gcd)/len1;ans = len1*n-ans;printf("%lld\n",ans);return 0 ;} 

总结:

   这题用的思维主要是:

                  1.求问题的反面。

                  2.cnt数组运用了桶计数的思想。空间换时间,1e6正好也开的下(对这个数字还是不太敏感啊你、、)(不对这题好像不是那个桶计数,,,这题cnt数组就开27 就够了)

 

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

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

相关文章

【牛客 - 181D】小叶的巡查(树的直径,数学)

题干&#xff1a; 8102年&#xff0c;牛客系列竞赛空前繁荣。为了更好地管理竞赛&#xff0c;小叶决定巡查于各大城市之间&#xff0c;体察民情。所以&#xff0c;从一个城市马不停蹄地到另一个城市成了小叶最常做的事情。小叶有一个钱袋&#xff0c;用于存放往来城市间的路费…

【蓝桥杯 - 练习】k倍区间(思维,数组)

题干&#xff1a; http://lx.lanqiao.cn/problem.page?gpidT444 问题描述 给定一个长度为N的数列&#xff0c;A1, A2, ... AN&#xff0c;如果其中一段连续的子序列Ai, Ai1, ... Aj(i < j)之和是K的倍数&#xff0c;我们就称这个区间[i, j]是K倍区间。   你能求出数列中…

【 HDU - 1525 】Euclid's Game(较难找规律,玄学博弈,分析必败点必胜点)

题干&#xff1a; Two players, Stan and Ollie, play, starting with two natural numbers. Stan, the first player, subtracts any positive multiple of the lesser of the two numbers from the greater of the two numbers, provided that the resulting number must be…

【CodeForces - 569A】Music (数学公式化简,模拟追及问题)

题干&#xff1a; Little Lesha loves listening to music via his smartphone. But the smartphone doesnt have much memory, so Lesha listens to his favorite songs in a well-known social network InTalk. Unfortunately, internet is not that fast in the city of E…

【CodeForces - 569B】Inventory (标记,乱搞)

题干&#xff1a; Companies always have a lot of equipment, furniture and other things. All of them should be tracked. To do this, there is an inventory number assigned with each item. It is much easier to create a database by using those numbers and keep …

【CodeForces - 371D】Vessels(思维,元素合并,并查集)

题干&#xff1a; There is a system of n vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 to n, in the order from the highest to the lowest, the volume of the i-th vessel is ai liters. Initially…

【UVA - 10891 Game of Sum 】【HRBUST - 1622】 Alice and Bob (区间dp,博弈问题)

题干&#xff1a; 有一个长度为N的整数序列&#xff0c;Alice和Bob轮流取数&#xff0c;Alice先取。每次玩家只能从左端或者右端 取一个或多个数&#xff0c;但不能两端都取。所有数都被取走后游戏结束&#xff0c;然后统计每个人取走的所有数之和&#xff0c; 作为各自的得分…

code iban 是有什么组成_EAN-128码和Code-128码的区别

什么是Code-128码&#xff1f;什么是EAN-128码&#xff1f;二者之间有什么区别&#xff1f;接下来小编就给大家解除心中的疑惑。Code-128码是一种高密度的条形码&#xff0c;可表示从 ASCII 0 到ASCII 127 共128个字符&#xff08;其中包含数字&#xff0c;字母&#xff0c;符号…

计算机中丢失setupxml.dll,Win7电脑安装VideoStudio Pro X6显示丢失SetupXML.dll文件怎么解决...

最近有win7系统用户在电脑安装VideoStudio Pro X6软件的时候&#xff0c;突然出现错误的提示&#xff0c;显示无法启动此程序&#xff0c;因为计算机中丢失SetupXML.dll。尝试重新安装该程序来解决此问题&#xff0c;要怎么办呢&#xff0c;下面给大家讲解一下Win7电脑安装软件…

怎么p出模糊的照片_36. 盲去卷积 - 更加实用的图像去模糊方法

本文同步发表在我的微信公众号和知乎专栏“计算摄影学”&#xff0c;欢迎扫码关注&#xff0c;上一篇文章35. 去卷积&#xff1a;怎么把模糊的图像变清晰&#xff1f;吸引了很多朋友的关注。在这篇文章里面&#xff0c;我给大家讲了一种叫做“非盲去卷积”的方法&#xff0c;当…

【CodeForces - 1038A 】Equality (思维水题,预处理字符串)

题干&#xff1a; You are given a string ss of length nn, which consists only of the first kk letters of the Latin alphabet. All letters in string ss are uppercase. A subsequence of string ss is a string that can be derived from ss by deleting some of its…

docker修改镜像的存储位置_云原生存储详解:容器存储与 K8s 存储卷(内含赠书福利)...

作者 | 阚俊宝 阿里巴巴技术专家参与文末留言互动&#xff0c;即有机会获得赠书福利&#xff01;导读&#xff1a;云原生存储详解系列文章将从云原生存储服务的概念、特点、需求、原理、使用及案例等方面&#xff0c;和大家一起探讨云原生存储技术新的机遇与挑战。本文为该系列…

vb简易计算机器程序,vb简易计算器源码

代码如下&#xff1a;/***Author:乌鸟heart*Version:1.0*/Dim IntX As Double 全局变量&#xff0c;用于存储计算的数值Dim IntOperation As Double 标记运算类型Dim isBegin As Boolean 标记是否已经给IntX赋值Public Sub Clear() 清空命令函数screen.Caption ""En…

【CodeForces - 1038B 】Non-Coprime Partition (构造,数组特征)

题干&#xff1a; Find out if it is possible to partition the first nn positive integers into two non-empty disjoint sets S1S1 and S2S2 such that: gcd(sum(S1),sum(S2))>1gcd(sum(S1),sum(S2))>1 Here sum(S)sum(S) denotes the sum of all elements presen…

计算机专业用锐龙笔记本,轻松应对工作挑战——ThinkPad T14 锐龙版,适合办公的笔记本电脑...

拥有一部适合办公的笔记本电脑&#xff0c;可以成为商务人士忙碌工作中强有力的支持。联想旗下的ThinkPad 系列笔记本电脑&#xff0c;一直秉持为高端商务人士服务的理念&#xff0c;以稳定、流畅、安全的使用体验得到广泛认可。其中的ThinkPad T14 锐龙版&#xff0c;更是有着…

python tabula 使用方法_Python中os.walk()的使用方法

os.walk()主要用来扫描某个指定目录下所包含的子目录和文件。这篇文章将通过几个简单的例子来说明python中os.walk()的使用方法。假设我们的test文件夹有如下的目录结构&#xff1a;我们首先用os.walk扫描test文件夹下所有的子目录和文件&#xff1a;# 使用os.walk扫描目录 imp…

【CodeForces - 1038C】Gambling (博弈问题,优先队列模拟,贪心)

题干&#xff1a; Two players A and B have a list of nn integers each. They both want to maximize the subtraction between their score and their opponents score. In one turn, a player can either add to his score any element from his list (assuming his list…

乐乐勇智能教育机器人有多少型号_【头条】协作机器人平台化趋势将会是柔性自动化的破局之道...

智能机器人商情微信公众号&#xff0c;关注中国智能机器人行业热点与发展趋势&#xff0c;打造快捷高效的行业资讯交互平台。更多精彩内容&#xff0c;您可以点击标题下方的蓝字关注我们。导语艾利特平台级CS系列协作机器人全新发布9月15日上海工博会第一天&#xff0c;艾利特机…

计算机毕设结束语致谢,毕业设计结束语和致谢

毕业设计结束语和致谢时间&#xff1a;2021-01-17 20:08:40 分类&#xff1a;小结范文 | 毕业论文致谢词范文 | Word文档下载毕业设计结束语和致谢导语&#xff1a;论文致谢应以简短的文字对课题研究与论文撰写过程中间直接给予帮助的人员(例如指导教师、答疑教师及其他人员)…

*【HDU - 2819】Swap(二分图匹配,输出路径)(待证明:是否是最少交换次数?)

题干&#xff1a; Given an N*N matrix with each entry equal to 0 or 1. You can swap any two rows or any two columns. Can you find a way to make all the diagonal entries equal to 1? Input There are several test cases in the input. The first line of each …