UVA 902 Password Search (字符串)

Password Search

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

A password with size N can be 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 are removed from the encoded text. Now, the password can be used to decode
the message.

Problem

Your mission has been simplified as you are 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 task, consider the following example in which the password size is three (N=3) and the text message is just baababacb. The password would then be aba because this is the substring with size 3 that appears most often in the
whole text (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 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 that the 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  


代码:
#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;
}

时间: 2024-08-24 20:25:42

UVA 902 Password Search (字符串)的相关文章

UVA - 902 Password Search

题目:给你一个小写字母组成大的串和一个整数n,找到里面长度为n出现最频繁的子串. 分析:字符串.hash表.字典树.这里使用hash函数求解,只做一次扫描即可. 说明:如果频率相同输出字典序最小的. #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> char subs[15],buf[1000001]; char *strsub(char *str, int n)

uva 1076 - Password Suspects(AC自动机+记忆化搜索)

题目链接:uva 1076 - Password Suspects 题目大意:有一个长度为n的密码,存在m个子串,问说有多少种字符串满足,如果满足个数不大于42,按照字典序输出. 解题思路:根据子串构建AC自动机,然后记忆化搜索,dp[i][u][s]表示第i个字符,在u节点,匹配s个子串. #include <cstdio> #include <cstring> #include <queue> #include <string> #include <

UVA - 10304Optimal Binary Search Tree(递推)

题目:UVA - 10304Optimal Binary Search Tree(递推) 题目大意:给出一组数,e1 < e2 < ... < en,现在要求将这些数组成一棵二叉搜索树,并且使得sum (ei * cost(ei))最小.cost(ei)表示ei到到根节点之间有多少条边. 解题思路:首先二叉搜索树要满足左节点小于根节点,右节点大于根节点.因此对于e1 < e2 < ... < en这样一组数,我们只要枚举根节点的位置ek,将这个序列分成左右子树两部分(e

UVa 489 Hangman Judge(字符串)

 Hangman Judge  In ``Hangman Judge,'' you are to write a program that judges a series of Hangman games. For each game, the answer to the puzzle is given as well as the guesses. Rules are the same as the classic game of hangman, and are given as follo

uva 1264 - Binary Search Tree(BST)

题目链接:uva 1264 - Binary Search Tree 题目大意:给定一个插入顺序,要求输出有多少种插入顺序,使得生成的BST一样. 解题思路:组合数学+BST的性质,起始左右两个子树的节点之间是没有影响的.所以逐层递推上去即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int max

UVA 1076 - Password Suspects(AC自动机+DP)

UVA 1076 - Password Suspects 题目链接 题意:一个密码,给定m个已知子串,求这个密码最多有几种表示方式,如果小于42种,就输出这些密码 思路:先利用已有子串构造AC自动机,需要改造一下的地方是每个叶子结点为(1<<i),然后构造next数组,在状态图上进行dp,dp[i][j][k]表示在结点i,长度j,已有子串集合为k的种数,进行状态转移即可,最后判断一下答案是否不大于42,如果是再根据之前求出的dp状态去进行输出即可 代码: #include <cstdi

UVA 1264 - Binary Search Tree(BST+计数)

UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树而言,仅仅要保证左边和右边顺序对就能够了,所以种数为C(左右结点总数,左结点),然后依据乘法原理乘上左右子树的情况就可以 代码: #include <cstdio> #include <cstring> typedef long long ll; const int MAXNODE =

【暑假】[数学]UVa 1262 Password

UVa 1262  Password 题目: Password Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Shoulder-surfing is the behavior of intentionally and stealthily watching the screen of another person's electronic de

uva 1584 Circular Sequence (字符串处理)

C - Circular Sequence Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Some DNA sequences exist in circular forms as in the following figure, which shows a circular sequence ``CGAGTCAGCT", that is,