2018年6月6日周中训练2

A.(点击打开链接

For an array b of length m we define the function f

as

f(b)={b[1]f(b[1]b[2],b[2]b[3],,b[m1]b[m])if m=1otherwise,

where

is bitwise exclusive OR.

For example, f(1,2,4,8)=f(12,24,48)=f(3,6,12)=f(36,612)=f(5,10)=f(510)=f(15)=15

You are given an array a

and a few queries. Each query is represented as two integers l and r. The answer is the maximum value of f on all continuous subsegments of the array al,al+1,,ar

.

Input

The first line contains a single integer n

( 1n5000) — the length of a

.

The second line contains n

integers a1,a2,,an ( 0ai2301

) — the elements of the array.

The third line contains a single integer q

( 1q100000

) — the number of queries.

Each of the next q

lines contains a query represented as two integers l, r ( 1lrn

).

Output

Print q

lines — the answers for the queries.

Examples
Input
3
8 4 1
2
2 3
1 2
Output
5
12
Input
6
1 2 4 8 16 32
4
1 6
2 5
3 4
1 2
Output
60
30
12
3
Note

In first sample in both queries the maximum value of the function is reached on the subsegment that is equal to the whole segment.

In second sample, optimal segment for first query are [3,6]

, for second query — [2,5], for third — [3,4], for fourth — [1,2].

题意:

求一个连续区间的最大值。连续区间的最大值可以是该区间的任何一个子连续区间的值。

思路:

发现规律这个题就可以用区间DP来解决,多写几个样例你会发现  dp[l][r]=dp[l+1][r]^dp[l][r-1]; 

然后利用区间DP求解即可。状态转移方程为:dp【i】【j】=max(dp【i】【j】,dp【i+1】【j】,dp【i】【j-1】);

代码:

#include <bits/stdc++.h>

using namespace std;
const int maxn=5500;
int a[maxn];
int dp[maxn][maxn];
int n;
void init()   //预处理,求得值
{
    for(int i=1;i<n;i++)
    {
        for(int l=1;l<=n-i;l++)
        {
            int r=l+i;
            dp[l][r]=dp[l+1][r]^dp[l][r-1];
        }
    }
}
int main()
{
    int q;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        dp[i][i]=a[i];  //初始化
    }
    scanf("%d",&q);
    init();
    for(int i=1;i<n;i++)
    {
        for(int l=1;l<=n-i;l++)
        {
            int r=l+i;
            dp[l][r]=max(dp[l][r],max(dp[l+1][r],dp[l][r-1]));  //找最大
        }
    }

    int u,v;
    while(q--)
    {
        scanf("%d%d",&u,&v);
        printf("%d\n",dp[u][v]);
    }
    return 0;
}

B、

题意:

给一个长度为n的字符串,问其子串中含有1 0个数相同的最长字串长度

思路:

思维题,真的考验智商。这个题和前缀和稍微联系一下,遇1加1遇0减1,其实就是和相等的两个数的长度一定就是相加为0的长度,所以记录每个和第一次出现的,然后下一次出现之后就更新。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#define maxn 1000010
using namespace std;
int n;
char str[100005];
struct node  //建立结构体
{
    int val;
    int id;
}pre[100005];
bool cmp(node a,node b)  //排序
{
    if(a.val==b.val) return a.id<b.id;
    return a.val<b.val;
}
int main()
{
    while(~scanf("%d",&n))
    {
        scanf("%s",str+1);
        pre[1].val=0;pre[1].id=1;
        for(int i=2;i<=n+1;i++)//注意,此处要算到n后面一个数
        {
            int x=str[i-1]=='0'?-1:1;
            pre[i].val=pre[i-1].val+x;
            pre[i].id=i;
        }
        sort(pre+1,pre+2+n,cmp);//按前缀和来排序,很重要
        int len=0,tmp=pre[1].val;
        for(int i=1;i<=n+1;i++)
        {
            int tmp=pre[i].val,p=pre[i].id;
            while(i<=n&&pre[i+1].val==tmp) i++;
            len=max(len,pre[i].id-p);
        }
        printf("%d\n",len);
    }
    return 0;
}

E、

You are given a string s consisting of |s| small english letters.

In one move you can replace any character of this string to the next character in alphabetical order (a will be replaced with b, s will be replaced with t, etc.). You cannot replace letter z with any other letter.

Your target is to make some number of moves (not necessary minimal) to get string abcdefghijklmnopqrstuvwxyz (english alphabet) as a subsequence. Subsequence of the string is the string that is obtained by deleting characters at some positions. You need to print the string that will be obtained from the given string and will be contain english alphabet as a subsequence or say that it is impossible.


Input

The only one line of the input consisting of the string s consisting of |s| (1 ≤ |s| ≤ 105) small english letters.

Output

If you can get a string that can be obtained from the given string and will contain english alphabet as a subsequence, print it. Otherwise print «-1» (without quotes).

Examples
Input
aacceeggiikkmmooqqssuuwwyy
Output
abcdefghijklmnopqrstuvwxyz
Input
thereisnoanswer
Output
-1

题意:判断某个字符串能否形成包含a-z的子串(不一定连续),每个字符除z以外均可被它下一个字符(a->b...),若能输出形成的串,否则输出-1

思路:

搞懂题意就很简单了,一开始没理解题意

代码:

#include <bits/stdc++.h>
using namespace std;
char s[100010];
int main()
{
    ios::sync_with_stdio(false);
    cin>>s;
    int N=strlen(s);
    char temp='a';
    bool flag=false;
    for(int i=0;i<N; i++)
    {
        if(s[i]<=temp)
        {
            s[i]=temp;
            temp++;
        }
        if(temp=='z'+1)
        {
            flag=true;
            break;
        }
    }
    if(flag==false)
    {
        cout<<"-1"<<endl;
        return 0;
    }
    for(int i=0;i<N;i++)
    {
            cout<<s[i];
    }
    cout<<endl;
    return 0;
}



猜你喜欢

转载自blog.csdn.net/sinat_37668729/article/details/80615753