http://acm.hnu.cn/online/?action=problem&type=show&id=12484&courseid=0
Unique Encryption Keys |
Time Limit: 30000ms, Special Time Limit:75000ms, Memory Limit:65536KB |
Total submit users: 19, Accepted users: 15 |
Problem 12484 : Special judge |
Problem description |
The security of many ciphers strongly depends on the fact that the keys are unique and never re-used. This may be vitally important, since a relatively strong cipher may be broken if the same key is used to encrypt several different messages.
In this problem, we will try to detect repeating (duplicate) usage of keys. Given a sequence of keys used to encrypt messages, your task is to determine what keys have been used repeatedly in some specified period. |
Input |
The input contains several cipher descriptions. Each description starts with one line containing two integer numbers M and Q separated by a space. M (1≤M≤1000000) is the number of encrypted messages, Q is the number of queries (0≤Q≤1000000). Each of the following M lines contains one number Ki (0≤i≤230) specifying the identifier of a key used to encrypt the i-th message. The next Q lines then contain one query each. Each query is specified by two integer numbers Bj and Ej, 1≤Bj≤Ej≤M, giving the interval of messages we want to check. There is one empty line after each description. The input is terminated by a line containing two zeros in place of the numbers M and Q. |
Output |
For each query, print one line of output. The line should contain the string ``OK" if all keys used to encrypt messages between Bj and Ej (inclusive) are mutually different (that means, they have different identifiers). If some of the keys have been used repeatedly, print one identifier of anysuch key.
Print one empty line after each cipher description. |
Sample Input |
10 5 3 2 3 4 9 7 3 8 4 1 1 3 2 6 4 10 3 7 2 6 5 2 1 2 3 1 2 2 4 1 5 0 0 |
Sample Output |
3 OK 4 3 OK OK 1 |
Problem Source |
Central Europe 2011 Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。 http://www.cnblogs.com/rooney/archive/2012/07/24/2606485.html c++map的使用方法 |
#include <iostream> #include <map> #include <vector> using namespace std; int min(int a,int b) { return a<b?a:b; } int main() { int m,q; int i; int x,y; while(scanf("%d%d",&m,&q)==2&&(m+q)) { vector<int > a(m); for(i=0;i<m;i++) scanf("%d",&a[i]); map<int ,int > key; //定义map 函数记录key vector<int > last(m); //last数组 记录从当前点最近一个match的点的位置 for(i=m-1;i>=0;i--) { last[i]=m; //最后一个点初始化为m if(i<m-1) last[i]=last[i+1]; //当前点初始化为当前的前一位置的最近match点 if(key.count(a[i])) last[i]=min(last[i],key[a[i]]); //如果当前点已经出现过,则选择当前点最近match点与当前点已经出现点中较小的 key[a[i]]=i; } for(i=0;i<q;i++) { scanf("%d%d",&x,&y); x--;y--; if(last[x]<=y) printf("%d\n",a[last[x]]); else printf("OK\n"); } printf("\n"); } return 0; } 学会用map,以及vector等stl模板函数 http://www.cnblogs.com/shiyangxt/archive/2008/09/11/1289493.html http://www.cnblogs.com/ffhajbq/archive/2012/07/16/2592926.html |