Codeforces Round #207 (Div. 2) Xenia and Hamming(字符串匹配)
2013-10-16 08:11
656 查看
http://codeforces.com/contest/357/problem/D
D. Xenia and Hamming
time limit per test
1 second
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.
Sample test(s)
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
思路:
s串azbc,t串gzxydh,串长ls=4,lt=6, 两长度的最大公约数gcd=2,最小公倍数lcm=12;将两个串都扩到长度为12;
az bc az
bc az bc
gz
xy dh gz xy dh
以gcd为单位长度将其分段,发现s串中字母si(0<=i<ls)位置为i%gcd(令x=i%gcd),则在lcm长度内,
其必与t串中字母tj(0<=j<lt)位置为j%gcd==x匹配一次;
如上述两字符串,在lcm长度内,s串首母a与t串中g,d,x各匹配一次;
D. Xenia and Hamming
time limit per test
1 second
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.
Sample test(s)
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
思路:
s串azbc,t串gzxydh,串长ls=4,lt=6, 两长度的最大公约数gcd=2,最小公倍数lcm=12;将两个串都扩到长度为12;
az bc az
bc az bc
gz
xy dh gz xy dh
以gcd为单位长度将其分段,发现s串中字母si(0<=i<ls)位置为i%gcd(令x=i%gcd),则在lcm长度内,
其必与t串中字母tj(0<=j<lt)位置为j%gcd==x匹配一次;
如上述两字符串,在lcm长度内,s串首母a与t串中g,d,x各匹配一次;
#include <string> #include <iostream> #include <cstdlib> using namespace std; typedef __int64 LL; const int V=1000100; char s[V],t[V]; int gcd(int x,int y) { if(!y)return x; return gcd(y,x%y); } LL n,m; int ct[V][26]; int main() { while(cin>>n>>m) { scanf("%s%s",s,t); int ls=strlen(s); int lt=strlen(t); int gd=gcd(ls,lt); LL cp=n*ls/((LL)ls/gd*lt); memset(ct,0,sizeof(ct)); for(int i=0;i<lt;i++) ct[i%gd][t[i]-'a']++; LL ret=(LL)ls/gd*lt; for(int i=0;i<ls;i++) ret-=ct[i%gd][s[i]-'a']; cout<<ret*cp<<endl; } return 0; }
相关文章推荐
- Codeforces Round #199 (Div. 2) A. Xenia and Divisors
- 【Codeforces Round 354 (Div 2)C】【前缀和二分or双指针】Vasya and String ab序列最多改变k位置的最长同字符子串长度
- Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations - 线段树
- Codeforces Round #253 (Div. 2), problem: (B)【字符串匹配】
- Codeforces Round #199 (Div. 2) B. Xenia and Spies(贪心)
- Codeforces Round #199 (Div. 2) B. Xenia and Spies
- Codeforces Round #199 (Div. 2) B. Xenia and Spies
- Codeforces Round #199 (Div. 2) E. Xenia and Tree
- 【Codeforces Round 263 (Div 2)B】【水题 贪心】Appleman and Card Game 选字符,每个字符个数的平方
- Codeforces Round #250 (Div. 1) A. The Child and Toy
- Codeforces Round #259 (Div. 2) A-Little Pony and Crystal Mine
- Codeforces Round #324 (Div. 2) C. Marina and Vasya(贪心)
- Codeforces Round #305 (Div. 2)B. Mike and Fun
- Codeforces Codeforces Round #432 (Div. 2 D ) Arpa and a list of numbers
- Codeforces Round #190 (Div. 2) B. Ciel and Flowers
- Codeforces Round #358 (Div. 2) B. Alyona and Mex 水题
- 【Codeforces Round 169 (Div 2) E】【数据结构区间维护】Little Girl and Problem on Trees 大菊花树的距离性修改查询
- 【Codeforces Round 263 (Div 2)C】【贪心 哈弗曼思维】Appleman and Toastman 每个非1size子树延展为2子树的最大权
- Codeforces Round #288 (Div. 2)---C. Anya and Ghosts
- Codeforces Round #227 (Div. 2)B. George and Round