CF204C.Little Elephant and Furik and Rubik
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Little Elephant loves Furik and Rubik, who he met in a small city Kremenchug.
The Little Elephant has two strings of equal length a and b, consisting only of uppercase English letters. The Little Elephant selects a pair of substrings of equal length — the first one from string a, the second one from string b. The choice is equiprobable among all possible pairs. Let's denote the substring of a as x, and the substring of b — as y. The Little Elephant gives string x to Furik and string y — to Rubik.
Let's assume that f(x, y) is the number of such positions of i (1 ≤ i ≤ |x|), that x__i = y__i (where |x| is the length of lines x and y, and x__i, y__i are the i-th characters of strings x and y, correspondingly). Help Furik and Rubik find the expected value of f(x, y).
小象喜欢它在小城克列缅丘格结识的弗里克和鲁比克。
小象有两个等长的字符串 a 和 b,均由大写英文字母组成。小象随机地选取一对等长的子串:第一个来自字符串 a,第二个来自字符串 b;所有可能的子串对被选中的概率均等。记从 a 中选出的子串为 x,从 b 中选出的子串为 y。小象将字符串 x 给弗里克,将字符串 y 给鲁比克。
定义 f(x,y) 为满足 xi=yi 的下标 i 的个数(其中 1≤i≤∣x∣;∣x∣ 表示字符串 x 和 y 的长度;xi、yi 分别表示字符串 x 和 y 的第 i 个字符)。请帮助弗里克和鲁比克求出 f(x,y) 的期望值。
输入格式
The first line contains a single integer n (1 ≤ n ≤ 2·105) — the length of strings a and b. The second line contains string a, the third line contains string b. The strings consist of uppercase English letters only. The length of both strings equals n.
第一行包含一个整数 n(1≤n≤2⋅105)—— 字符串 a 和 b 的长度。
第二行包含字符串 a,第三行包含字符串 b。
两个字符串仅由大写英文字母组成,且长度均为 n。
输出格式
On a single line print a real number — the answer to the problem. The answer will be considered correct if its relative or absolute error does not exceed 10 - 6.
在单行中输出一个实数——该问题的答案。只要答案的相对误差或绝对误差不超过 10−6,即视为正确。
输入输出样例
输入#1
2 AB BA
输出#1
0.400000000
输入#2
3 AAB CAA
输出#2
0.642857143
说明/提示
Let's assume that we are given string a = _a_1_a_2... a|a|, then let's denote the string's length as |a|, and its i-th character — as a__i.
A substring a[l... r] (1 ≤ l ≤ r ≤ |a|) of string a is string a__l__a__l + 1... a__r.
String a is a substring of string b, if there exists such pair of integers l and r (1 ≤ l ≤ r ≤ |b|), that b[l... r] = a.
Let's consider the first test sample. The first sample has 5 possible substring pairs: ("A", "B"), ("A", "A"), ("B", "B"), ("B", "A"), ("AB", "BA"). For the second and third pair value f(x, y) equals 1, for the rest it equals 0. The probability of choosing each pair equals
, that's why the answer is
· 0 +
· 1 +
· 1 +
· 0 +
· 0 =
= 0.4.
假设我们给定字符串 a=a1a2…a∣a∣,则记该字符串的长度为 ∣a∣,其第 i 个字符为 ai。
字符串 a 的子串 a[l…r](其中 1≤l≤r≤∣a∣)定义为字符串 alal+1…ar。
若存在整数对 l 和 r(满足 1≤l≤r≤∣b∣),使得 b[l…r]=a,则称字符串 a 是字符串 b 的一个子串。
我们考虑第一个测试样例。该样例共有 5 种可能的子串对:("A", "B")、("A", "A")、("B", "B")、("B", "A")、("AB", "BA")。其中,第二和第三对的函数值 f(x,y) 等于 1,其余各对的函数值均为 0。每一对被选中的概率均为
,因此答案为
· 0 +
· 1 +
· 1 +
· 0 +
· 0 =
= 0.4。
输入解题思路,AI测评打分。不知道怎么写?