codeforces-204c Little Elephant and Furik and Rubik

Source: Internet
Author: User

codeforces-204c Little Elephant and Furik and Rubik

Personal feeling is a good question.

This problem at first glance we can not start, then we first think about how to fight violence

Is violence not simple? Enumerate all strings, enumerate all the locations, and figure out all the answers.

We naturally cannot have no brain violence, but violence can inspire us.

We know that all the characters that contribute to the answer must be the same ( nonsense )

So we can O (n^2) first enumerate the same characters in two strings and then consider how to contribute

Then calculate the value of all the schemes, divided by N (n+1) * (2*n+1)/6 [Don't know how to get to wall]

We found that if these two identical characters are to contribute, they must be in the same position as the string

In other words, the characters on the left side of the two characters must be the same number (the same character must be the same at the right).

For example: (Subscript all starts from 1)

S1:abbaca

S2:baabac

In this case, if s1[1],s2[2] two identical characters ' a ' to contribute, then the left must have 0,1, 2, 3 ...

We found that none of the characters on the left could have

What if S1[3],S2[4] make a contribution? There can be only 2 characters on the left: because the lowest value of the subscript is 3,3-1=2

So we find that the maximum number of characters left is only related to the leftmost character.

Accordingly, the maximum number of characters to the right of each time is only related to the most right character.

So, what's the contribution??

We can enumerate the most right characters, and then calculate and contribute to the answers to the characters on his left.

If we have now identified s1[4], consider making it a contribution.

In the case of <= 4

S2[2] Make a contribution: (6-4) * (2)??

S2[3] Make a contribution: (6-4) * (3)??

Then we find that each contribution relates only to the sum of the subscript of the <= Pos character in another string

So we sweep the line over and over, the process accumulates the subscript and the direct O (n) calculation can be

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5typedefLong Longll;6InlinevoidReadint&x) {7x=0;CharChBOOLFlag =false;8      while(Ch=getchar (),ch<'!');if(ch = ='-') Ch=getchar (), flag =true;9      while(x=Ten*x+ch-'0', Ch=getchar (),ch>'!');if(flag) x=-x;Ten } OneInlineintCat_max (Const int&a,Const int&AMP;B) {returnA>b?a:b;} AInlineintCat_min (Const int&a,Const int&AMP;B) {returnA<b?a:b;} - Const intMAXN =200010; - CharS1[MAXN],S2[MAXN]; the Doublea[ the],b[ the]; - intMain () { -     intn;read (n); -scanf"%s%s", s1+1, s2+1); +     DoubleAns =0; -      for(intI=1; i<=n;++i) { +B[s2[i]] + =i; AAns + = 1ll* (n-i+1) * (A[s2[i]] +B[s1[i]]); atA[s1[i]] + =i; -     } -printf"%.20lf\n",6.0*ans/n/(n+1)/(n<<1|1)); - GetChar (); GetChar (); -     return 0; -}
View Code

codeforces-204c Little Elephant and Furik and Rubik

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.