训练一

一、A - Vicious Keyboard

Tonio has a keyboard with only two letters, "V" and "K".

One day, he has typed out a string s with only these two letters. He really likes it when the string "VK" appears, so he wishes to change at most one letter in the string (or do no changes) to maximize the number of occurrences of that string. Compute the maximum number of times "VK" can appear as a substring (i. e. a letter "K" right after a letter "V") in the resulting string.

Input

The first line will contain a string s consisting only of uppercase English letters "V" and "K" with length not less than 1 and not greater than 100.

Output

Output a single integer, the maximum number of times "VK" can appear as a substring of the given string after changing at most one character.

Input

The first line will contain a string s consisting only of uppercase English letters "V" and "K" with length not less than 1 and not greater than 100.

Output

Output a single integer, the maximum number of times "VK" can appear as a substring of the given string after changing at most one character.

Note

For the first case, we do not change any letters. "VK" appears once, which is the maximum number of times it could appear.

For the second case, we can change the second character from a "V" to a "K". This will give us the string "VK". This has one occurrence of the string "VK" as a substring.

For the fourth case, we can change the fourth character from a "K" to a "V". This will give us the string "VKKVKKKKKKVVVVVVVVVK". This has three occurrences of the string "VK" as a substring. We can check no other moves can give us strictly more occurrences.


 题意:在给出 字符串中,只能修改一次,可以组成多少对VK


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stack>
#include <map>
#include <queue>
using namespace std;
int main()
{
   char mapp[110];
   int vis[110];
   int sum;
   while(scanf("%s",mapp)!=EOF)
   {
       memset(vis, 0, sizeof vis);
       int len=strlen(mapp);
       int sum=0;
        //在未修改之前,找出有多少对VK,并作标记;
       for(int i=0;i<len-1;i++)
       {
           if(mapp[i]=='V'&&mapp[i+1]=='K')
           {
               vis[i]=vis[i+1]=1;
               sum++;
               i++;
           }
       }
        //找出没有做标记的VV,或者KK,加1结束(因为只能修改一次)
       for(int i=0; i<len-1; i++)
       {
           if((mapp[i]=='V'||mapp[i+1]=='K')&&vis[i]==0&&vis[i+1]==0)
           {
               sum++;
               break;
           }
       }
       printf("%d\n",sum);
   }
}

二、B - Odd sum

You are given sequence a1, a2, ..., an of integer numbers of length n. Your task is to find such subsequence that its sum is odd and maximum among all such subsequences. It's guaranteed that given sequence contains subsequence with odd sum.

Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.

You should write a program which finds sum of the best subsequence.

Input

The first line contains integer number n (1 ≤ n ≤ 105).

The second line contains n integer numbers a1, a2, ..., an ( - 104 ≤ ai ≤ 104). The sequence contains at least one subsequence with odd sum.

Output

Print sum of resulting subseqeuence.

Examples

Input

4
-2 2 -3 1

Output

3

Input

3
2 -5 -3

Output

-1

Note

In the first example sum of the second and the fourth elements is 3.


题意:在输入的所有数中,输出最大的和(且为奇数)

一开始wa 想成奇数和偶数只能成对存在在和中。。。。


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stack>
using namespace std;
int n, sum, sum1 = 999999999, sum2 = -999999999;
int ans1 =-0x7f7f7f7f;
int ans2 =-0x7f7f7f7f;
int mapp[100010];
int cmp(int x, int y)
{
    return x>y;
}
int main()
{
    while(scanf("%d", &n)!=EOF)
    {
        for(int i=0; i<n; i++)
        {
            scanf("%d", &mapp[i]);
            将所有正数全部相加,并找出正奇数中最小的和负奇数中最大的;
            if(mapp[i]>0)
            {
                sum+=mapp[i];
                if(mapp[i]%2!=0)
                {
                    sum1 = min(sum1, mapp[i]);
                }
            }
            else if(mapp[i]<0)
            {
                if(mapp[i]%2!=0)
                {
                    sum2 = max(sum2, mapp[i]);
                }
            }
        }
        如果所有正数加起来的和正好为奇数,则输出即可,否则判断是减去最小正奇数大还是加上最大负奇数大;
        if(sum%2!=0)
            printf("%d\n", sum);
        else
        {
            ans1 = sum-sum1;
            ans2 = sum+sum2;
            int ans = max(ans1, ans2);
            printf("%d\n", ans);
        }
        return 0;
    }
}

三、C - Japanese Crosswords Strike Back

A one-dimensional Japanese crossword can be represented as a binary string of length x. An encoding of this crossword is an array a of size n, where n is the number of segments formed completely of 1's, and ai is the length of i-th segment. No two segments touch or intersect.

For example:

  • If x = 6 and the crossword is 111011, then its encoding is an array {3, 2};
  • If x = 8 and the crossword is 01101010, then its encoding is an array {2, 1, 1};
  • If x = 5 and the crossword is 11111, then its encoding is an array {5};
  • If x = 5 and the crossword is 00000, then its encoding is an empty array.

Mishka wants to create a new one-dimensional Japanese crossword. He has already picked the length and the encoding for this crossword. And now he needs to check if there is exactly one crossword such that its length and encoding are equal to the length and encoding he picked. Help him to check it!

Input

The first line contains two integer numbers n and x (1 ≤ n ≤ 100000, 1 ≤ x ≤ 109) — the number of elements in the encoding and the length of the crossword Mishka picked.

The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 10000) — the encoding.

Output

Print YES if there exists exaclty one crossword with chosen length and encoding. Otherwise, print NO.

Examples

Input

2 4
1 3

Output

NO

Input

3 10
3 3 2

Output

YES

Input

2 10
1 3

Output

NO

题意:往x个0里,分组填1(a1,a2...), 判断是否存在合法的(每组1中间有一个0间隔)

给了段数和长度,以及每一段的长度,问是否只有唯一一种情况,所以只要满足a1+a2+...+an=x-n+1即可

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stack>
using namespace std;
int n, x;
int sum;
int mapp[100010];
int main()
{
    while(scanf("%d%d", &n, &x)!=EOF)
    {
        sum = 0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&mapp[i]);
            sum+=mapp[i];
        }
        if(sum+n-1==x)
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
}

四、Bertown Subway

The construction of subway in Bertown is almost finished! The President of Berland will visit this city soon to look at the new subway himself.

There are n stations in the subway. It was built according to the Bertown Transport Law:

  1. For each station i there exists exactly one train that goes from this station. Its destination station is pi, possibly pi = i;
  2. For each station i there exists exactly one station j such that pj = i.

The President will consider the convenience of subway after visiting it. The convenience is the number of ordered pairs (x, y) such that person can start at station x and, after taking some subway trains (possibly zero), arrive at station y(1 ≤ x, y ≤ n).

The mayor of Bertown thinks that if the subway is not convenient enough, then the President might consider installing a new mayor (and, of course, the current mayor doesn't want it to happen). Before President visits the city mayor has enough time to rebuild some paths of subway, thus changing the values of pi for not more than two subway stations. Of course, breaking the Bertown Transport Law is really bad, so the subway must be built according to the Law even after changes.

The mayor wants to do these changes in such a way that the convenience of the subway is maximized. Help him to calculate the maximum possible convenience he can get!

Input

The first line contains one integer number n (1 ≤ n ≤ 100000) — the number of stations.

The second line contains n integer numbers p1, p2, ..., pn (1 ≤ pi ≤ n) — the current structure of the subway. All these numbers are distinct.

Output

Print one number — the maximum possible value of convenience.

Examples

Input

3
2 1 3

Output

9

Input

5
1 5 4 3 2

Output

17

Note

In the first example the mayor can change p2 to 3 and p3 to 1, so there will be 9pairs: (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3).

In the second example the mayor can change p2 to 4 and p3 to 5.


题意:n代表有几个车站,pi = j 代表i车站通j车站,通过交换两个车站,得到最多数的有序对, 例如示例1:p1=2, p2=1, p3=3, 说明从1到2通,从2到1通,从3到3通;通过交换p2,p3;得到p1=2, p2=3, p3=1;(从1到2通,从2到3通,从3到1通)⇨(从1到1通,从1到2通,从1到3,从2到2,从2到3,从2到1,从3到3,从3到1,从3到2)共九条,所以 我们可以推出,只要选出两个包含的车站数最多的环即可((a+b)^2>=a^2+b^2)。


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stack>
#include<vector>
using namespace std;
int main()
{
    vector<int>que;
    int mapp[100010];
    bool vis[100010];
    long long n;
    scanf("%lld",&n);
    for(int i=1; i<=n; i++)
    {
        scanf("%d", &mapp[i]);
    }
    que.clear();//清空que中的所有元素;
    memset(vis, false, sizeof vis);
    for(int i=1; i<=n; i++)
    {
        if(vis[i]==true)
            continue;
        int sum = 1;
        vis[i] = true;
        int x = mapp[i];
        //x=i时,才说明组成该环的车站数已经计数完成,并把车站数存入que的尾部;
        while(x!=i)
        {
            vis[x] = true;
            x = mapp[x];
            sum++;
        }
        que.push_back(sum);
    }
    long long ans = 0;
    sort(que.begin(), que.end());//对于环的大小进行排序;
    if(que.size()>=2)//如果最后组成的环有两个或两个以上,就要取最大的两个环相加;
    {
        ans = que[que.size()-1]+que[que.size()-2];//因为为升序排序,所以最大的两个环为..;
        ans*=ans;
        //除了最大的两个环,其他的环就为que[j]*que[j];
        for(int j=0; j<que.size()-2; j++)
        {
            ans+=que[j]*que[j];
        }
    }
    else//如果只有一个环,说明组成该环的车站数为n,就直接为n*n;
        ans = n*n;
    printf("%lld\n", ans);
    return 0;
}

五、Ancient Cipher

 Ancient Roman empire had a strong government system with various departments, including a secret service department. Important documents were sent between provinces and the capital in encrypted form to prevent eavesdropping. The most popular ciphers in those times were so called substitution cipher and permutation cipher. 
Substitution cipher changes all occurrences of each letter to some other letter. Substitutes for all letters must be different. For some letters substitute letter may coincide with the original letter. For example, applying substitution cipher that changes all letters from 'A' to 'Y' to the next ones in the alphabet, and changes 'Z' to 'A', to the message "VICTORIOUS" one gets the message "WJDUPSJPVT". 
Permutation cipher applies some permutation to the letters of the message. For example, applying the permutation <2, 1, 5, 4, 3, 7, 6, 10, 9, 8> to the message "VICTORIOUS" one gets the message "IVOTCIRSUO". 
It was quickly noticed that being applied separately, both substitution cipher and permutation cipher were rather weak. But when being combined, they were strong enough for those times. Thus, the most important messages were first encrypted using substitution cipher, and then the result was encrypted using permutation cipher. Encrypting the message "VICTORIOUS" with the combination of the ciphers described above one gets the message "JWPUDJSTVP". 
Archeologists have recently found the message engraved on a stone plate. At the first glance it seemed completely meaningless, so it was suggested that the message was encrypted with some substitution and permutation ciphers. They have conjectured the possible text of the original message that was encrypted, and now they want to check their conjecture. They need a computer program to do it, so you have to write one.

Input

Input contains two lines. The first line contains the message engraved on the plate. Before encrypting, all spaces and punctuation marks were removed, so the encrypted message contains only capital letters of the English alphabet. The second line contains the original message that is conjectured to be encrypted in the message on the first line. It also contains only capital letters of the English alphabet. 
The lengths of both lines of the input are equal and do not exceed 100.

Output

Output "YES" if the message on the first line of the input file could be the result of encrypting the message on the second line, or "NO" in the other case.

Sample Input

JWPUDJSTVP
VICTORIOUS

Sample Output

YES

题意:输入两个字符串,让你对其中一个字符串操作。操作内容是,可以将这个字符串任意排列,然后将这个字符串中所有相同的字符随意映射成其他一种字符,使得映射后的字符串与另一个字符串相同。

所以只需要将所有相同字符的数量记录下来,与另一个字符串比较数量是否是一一对应的即可。


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stack>
using namespace std;
char mapp1[100], mapp2[100];
int flag1[26], flag2[26];
int main()
{
    while(scanf("%s%s", mapp1, mapp2)!=EOF)
    {
        memset(flag1, 0, sizeof(flag1));
        memset(flag2, 0, sizeof(flag2));
        int len1 = strlen(mapp1);
        int len2 = strlen(mapp2);
        for(int i=0; i<len1; i++)
        {
            flag1[mapp1[i]-'A']++;
            flag2[mapp2[i]-'A']++;
        }
        sort(flag1, flag1+26);
        sort(flag2, flag2+26);
        for(int i=0; i<26; i++)
        {
            if(flag1[i]!=flag2[i])
            {
                printf("NO\n");
                return 0;
            }
        }
        printf("YES\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42569807/article/details/88920314