UVA 902 Password Search (String)

Source: Internet
Author: User



Password Search

Being able to send encoded messages during World War II were very important to the Allies. The messages were always sent after being encoded with a known password. Having a fixed password is of course insecure, thus there is a need to the change it frequently. However, a mechanism is necessary to send the new password. One of the mathematicians working in the cryptographic team had a clever idea of is to send the password hidden within The message itself. The interesting point is that the receiver of the message is had to know the size of the password and then search for T He password within the received text.

A password with size N can is found by searching the text for the most frequent substring with n characters. After finding the password, all the substrings that coincide with the password is removed from the encoded text. Now, the password can is used to decode the message.

problemYour Mission has been simplified as is only requested to write a program that, given the size of the password and The encoded message, determines the password following the strategy given above.

To illustrate your tasks, consider the following example in which the password size are three (n=3) and the text message is Just BAABABACB. The password would then was ABA because this was the substring with a size 3 that appears most often in the whole Tex T (it appears twice) while the other six different substrings appear only once (baa ; AaB ; Bab ; BAC ; ACB).

Input

The input file contains several test cases, each of the them consists of one line with the size of the password, 0 < n≤10 , followed by the text representing the encoded message. To simplify things, you can assume this text only includes lower case letters.

Output

For each test case, your program should print as output a line with the password string.

Sample Input
3 BAABABACB
Sample Output
Aba  
Code:
#include <iostream> #include <string> #include <map>using namespace std;string Str0,ans,str1;map < string,int> Mymap;int Main () {    int n;    while (cin>>n) {        int maxn=-1;        Mymap.clear ();        cin>>str0;        for (int i=0;i<=str0.length ()-n;i++) {            str1=str0.substr (i,n);            mymap[str1]++;        }        Map<string,int>::iterator it;        For (It=mymap.begin (); It!=mymap.end (); it++) {            if (IT->SECOND>MAXN) {                maxn=it->second;                ans=it->first;            }        }        cout<<ans<<endl;    }    return 0;}



UVA 902 Password Search (String)

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.