Solution to the tle problem caused by violent swap

Source: Internet
Author: User

B. Pasha and String

The topic is simple, that is, the string from one position to another position between the fragments in reverse order, the basic idea is to use swap 22 exchange. At this point the problem is the time limit exceeded, 22 exchange too time-consuming, because there is exchange in the past and exchange back to the equivalent of no change in exchange wasted time, there is no better way? Because the upside is the middle number, so you can count the number of times each letter in the fragment is reversed, if it is odd several times, then the number of times will not be exchanged. There is also a place to note, because the topic is prompted, each time the AI is less than or equal to One-second times the length of the string, so the statistical reversal of the number of times the loop variable to reach half the length of the string. Because it is a fragment, the characters in the middle of the fragment are reversed, so there are a[i] + + a[i-1], before there are similar statistical methods, should learn.

1#include <iostream>2#include <string>3 using namespacestd;4 inta[100005];5 intMain ()6 {7     strings;8CIN >>s;9     intLen =s.size ();Ten     intm; OneCIN >>m; A      for(inti =0; I < m; i++)  -     { -         intT; theCIN >>T; -a[t]++; -     } -      for(inti =1; I <= Len/2; i++)  +A[i] + = a[i-1]; -      for(inti =0; I <= Len/2; i++)  +     if(A[i] &1)  ASwap (s[i-1], S[len-i]); atcout << S <<Endl; -     return 0; -}

  

  

Solution to the tle problem caused by violent swap

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.