Unique Encryption Keys

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 ≤ Ki ≤ 2 30) 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 any such key. Print one empty line after each cipher description.

Sample Input

10 5

3

2

3

4

9

7

3

8

4

1

3

2 6

4 10

3 7 2 6

2

1

2

3

1

2

2 4

1 5

0 0

Sample Output

3

OK

4

3

OK

OK

1

 

// 题意,m长的序列,要q次询问,然后m个数,再q对 l,r ,问当中有没有重复的,有就输出最靠左的

//f[i] 表示i位置右边最近发生重复的位置,就很简单了,主要是没想到啊!

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <map>
 5 using namespace std;
 6 #define MX 1000005
 7 
 8 int a[MX];
 9 int f[MX]; //f[i] 表示i位置右边最近发生重复的位置
10 
11 int main()
12 {
13     int n,q;
14     while (scanf("%d%d",&n,&q)&&(n+q))
15     {
16         map <int ,int> mp;
17         for (int i=1;i<=n;i++)
18             scanf("%d",&a[i]);
19         f[n]=n+1;
20         mp[a[n]]=n;
21         for (int i=n-1;i>=1;i--)
22         {
23             if (mp.count(a[i]))
24                 f[i] = min(f[i+1],mp[a[i]]);
25             else
26                 f[i] = f[i+1];
27             mp[a[i]]=i;
28         }
29         while (q--)
30         {
31             int l,r;
32             scanf("%d%d",&l,&r);
33             if (f[l]<=r)
34                 printf("%d\n",a[f[l]]);
35             else
36                 printf("OK\n");
37         }
38         printf("\n");
39     }
40     return 0;
41 }
View Code

 

转载于:https://www.cnblogs.com/haoabcd2010/p/7203799.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值