QDU_AP协会18级ST1

A - A + B Problem II

I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. 

Input

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000. 

Output

For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases. 

Sample Input

2
1 2
112233445566778899 998877665544332211

Sample Output

Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

思路:高精,套了个高精的板子

代码,不写了,不然篇幅过长,同样适用D题

B - Fire!

Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze. Given Joe’s location in the maze and which squares of the maze are on fire, you must determine whether Joe can exit the maze before the fire reaches him, and how fast he can do it. Joe and the fire each move one square per minute, vertically or horizontally (not diagonally). The fire spreads all four directions from each square that is on fire. Joe may exit the maze from any square that borders the edge of the maze. Neither Joe nor the fire may enter a square that is occupied by a wall. Input The first line of input contains a single integer, the number of test cases to follow. The first line of each test case contains the two integers R and C, separated by spaces, with 1 ≤ R, C ≤ 1000. The following R lines of the test case each contain one row of the maze. Each of these lines contains exactly C characters, and each of these characters is one of: • #, a wall • ., a passable square • J, Joe’s initial position in the maze, which is a passable square • F, a square that is on fire There will be exactly one J in each test case. Output For each test case, output a single line containing ‘IMPOSSIBLE’ if Joe cannot exit the maze before the fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.

Sample Input 2 4 4 #### #JF# #..# #..# 3 3 ### #J. #.F

Sample Output 3 IMPOSSIBLE

思路,两次bfs,第一次把火的可以到达的点的时间更新上,第二次去bfs去跑人的逃生(坑点在于题目中火的位置不是一个)

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<cmath>
#define INF 


typedef long long ll;
using namespace std;
int n,m,sx,sy,sx1,sy1;
char Map[1005][1005];
int conte[1005][1005];
int vis1[1005][1005];
int dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
ll s=0; 
struct node
{
	int x,y;
	int step;
};
queue<node>q1;
void bfs1() 
{

	while(!q1.empty())
	{
		node now;
		now=q1.front();
		q1.pop();
		for(int t=0;t<4;t++)
		{
			node next;
			next.x=now.x+dir[t][0];
			next.y=now.y+dir[t][1];
			next.step=now.step+1;
			if((Map[next.x][next.y]=='.'||Map[next.x][next.y]=='J')&&next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&vis1[next.x][next.y]==0)
			{
			
				conte[next.x][next.y]=next.step;
				vis1[next.x][next.y]=1;
				q1.push(next);
			}
		}
	}
	
}
int bfs2(int x,int y)
{
      node start;
      start.x=x;
      start.y=y;
      start.step=0;
      vis1[x][y]=1;
      queue<node>q2;
      q2.push(start);
      while(!q2.empty())
      {
      	node now;
      	now=q2.front();
      	q2.pop();
      	if(now.x==0||now.x==n-1||now.y==0||now.y==m-1)
	   {
			    s=now.step+1;
			  	return 1;
	   }	
      	for(int t=0;t<4;t++)
      	{
      	   node next;
		   next.x=now.x+dir[t][0];
		   next.y=now.y+dir[t][1];
		   next.step=now.step+1;
		   if(Map[next.x][next.y]=='.'&&next.step<conte[next.x][next.y]&&vis1[next.x][next.y]==0)
		   {
			  	vis1[next.x][next.y]=1; 
			  	q2.push(next);
		   }
		   
		}
	  }
	  return 0;
}
int main()
{
	int T;
	cin>>T;
	
	while(T--)
	{
		cin>>n>>m;
	    getchar();
	    while(!q1.empty())
		{
			q1.pop();
		 } 
	    for(int t=0;t<n;t++)
	    {
	    	scanf("%s",Map[t]);
		}
		for(int t=0;t<n;t++)
		{
			for(int j=0;j<m;j++)
			{
				if(Map[t][j]=='J')
				{
					sx=t;
					sy=j;
				}
				if(Map[t][j]=='F')
				{
					node p;
					p.x=t;
					p.y=j;
					p.step=0;
					vis1[t][j]=1;
					q1.push(p);
				}
			}
		}
		for(int t=0;t<n;t++)
		{
			for(int j=0;j<m;j++)
			{
				conte[t][j]=1000000007;
			}
		}
		memset(vis1,0,sizeof(vis1));
		bfs1();
		memset(vis1,0,sizeof(vis1));

    	if(bfs2(sx,sy))
    	{
    		cout<<s<<endl;
		}
		else
		{
			cout<<"IMPOSSIBLE"<<endl;
		}	
	}
	return 0;
}

C - Virtual Friends

These days, you can do all sorts of things online. For example, you can use various websites to make virtual friends. For some people, growing their social network (their friends, their friends' friends, their friends' friends' friends, and so on), has become an addictive hobby. Just as some people collect stamps, other people collect virtual friends. 

Your task is to observe the interactions on such a website and keep track of the size of each person's network. 

Assume that every friendship is mutual. If Fred is Barney's friend, then Barney is also Fred's friend.

Input

Input file contains multiple test cases. 
The first line of each case indicates the number of test friendship nest. 
each friendship nest begins with a line containing an integer F, the number of friendships formed in this frindship nest, which is no more than 100 000. Each of the following F lines contains the names of two people who have just become friends, separated by a space. A name is a string of 1 to 20 letters (uppercase or lowercase).

Output

Whenever a friendship is formed, print a line containing one integer, the number of people in the social network of the two people who have just become friends.

Sample Input

1
3
Fred Barney
Barney Betty
Betty Wilma

Sample Output

2
3
4

思路:把他子结点的数量更新到父节点上就ok了,注意map的使用

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>


typedef long long ll;
using namespace std;

map<string,int>mp;
int  pre[200005];
int num[200005];
int  find(int x)
{
	if(x==pre[x])
	{
		return x;
	}
	else 
	{
		return pre[x]=find(pre[x]);
	}
 } 
 void Merge(int x,int y)
 {
 	int fx=find(x);
 	int fy=find(y);
 	if(fx!=fy)
 	{
 	   	pre[fx]=fy;
 	   	num[fy]+=num[fx];
	}
 }
int main()
{
	int T;
	while(cin>>T)
	{
	
	while(T--)
	{
		int n;
		cin>>n;
		string s1,s2;
		int cnt=1;
		mp.clear();
		for(int t=1;t<=200000;t++)
		{
			pre[t]=t;
			num[t]=1;
		}
		for(int t=0;t<n;t++)
		{
			cin>>s1>>s2;
			if(mp[s1]==0)
			{
				mp[s1]=cnt;
				cnt++;
			}
			if(mp[s2]==0)
			{
			     mp[s2]=cnt;
			     cnt++;
			}
			Merge(mp[s1],mp[s2]);
			cout<<num[find(mp[s1])]<<endl;
		}
	}
	} 
	return 0;
 } 

E - Cube Stacking

Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes labeled 1 through N. They start with N stacks, each containing a single cube. Farmer John asks Betsy to perform P (1<= P <= 100,000) operation. There are two types of operations: 
moves and counts. 
* In a move operation, Farmer John asks Bessie to move the stack containing cube X on top of the stack containing cube Y. 
* In a count operation, Farmer John asks Bessie to count the number of cubes on the stack with cube X that are under the cube X and report that value. 

Write a program that can verify the results of the game. 

Input

* Line 1: A single integer, P 

* Lines 2..P+1: Each of these lines describes a legal operation. Line 2 describes the first operation, etc. Each line begins with a 'M' for a move operation or a 'C' for a count operation. For move operations, the line also contains two integers: X and Y.For count operations, the line also contains a single integer: X. 

Note that the value for N does not appear in the input file. No move operation will request a move a stack onto itself. 

Output

Print the output from each of the count operations in the same order as the input file. 

Sample Input

6
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4

Sample Output

1
0
2

这个题比较复杂一点,看了好久,才有了点思路,借鉴了一下网上的思路,明白了过程,就是开两个数组,一个存集合的数目,每次查找的时候要把他之前的sum总的更新一下,并且要注意之后输出之前也要自己跑一次find更新

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<set> 
#include<map>
#include<vector>
#include<cmath>

typedef long long ll;
using namespace std;

int pre[30005];
int num[30005];
int sum[30005];

int find(int x)
{
	if(x==pre[x])
	{
		return x;
	}
	else
	{
		int t=find(pre[x]);
		sum[x]+=sum[pre[x]];
		pre[x]=t;
		return t;
	}
}
void merge(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	{
		pre[fx]=fy;
		sum[fx]+=num[fy];
		num[fy]+=num[fx];
	}
}
int main()
{
	for(int t=1;t<=30000;t++)
	{
		pre[t]=t;
		num[t]=1;
	}
	int P;
	cin>>P;
	getchar();
	for(int t=0;t<P;t++)
	{
		char str[10];
		scanf("%s",str);
		if(str[0]=='M')
		{
			int a,b;
			scanf("%d%d",&a,&b);
			merge(a,b);
		}
		else
		{
			int a;
			scanf("%d",&a);
		    find(a);
			printf("%d
",sum[a]);
		}
	}
	
	return 0;
}

递推的几篇我就附上DKS大佬的博客链接吧 

https://blog.csdn.net/qq_42936517/article/details/88133155

https://blog.csdn.net/qq_42936517/article/details/88130640

G - To Add or Not to Add

A piece of paper contains an array of n integers a1, a2, ..., an. Your task is to find a number that occurs the maximum number of times in this array.

However, before looking for such number, you are allowed to perform not more than kfollowing operations — choose an arbitrary element from the array and add 1 to it. In other words, you are allowed to increase some array element by 1 no more than ktimes (you are allowed to increase the same element of the array multiple times).

Your task is to find the maximum number of occurrences of some number in the array after performing no more than k allowed operations. If there are several such numbers, your task is to find the minimum one.

Input

The first line contains two integers n and k (1 ≤ n ≤ 105; 0 ≤ k ≤ 109) — the number of elements in the array and the number of operations you are allowed to perform, correspondingly.

The third line contains a sequence of n integers a1, a2, ..., an (|ai| ≤ 109) — the initial array. The numbers in the lines are separated by single spaces.

Output

In a single line print two numbers — the maximum number of occurrences of some number in the array after at most k allowed operations are performed, and the minimum number that reaches the given maximum. Separate the printed numbers by whitespaces.

Examples

Input

5 3
6 3 4 0 2

Output

3 4

Input

3 4
5 5 5

Output

3 5

Input

5 3
3 1 2 2 1

Output

4 2

Note

In the first sample your task is to increase the second element of the array once and increase the fifth element of the array twice. Thus, we get sequence 6, 4, 4, 0, 4, where number 4 occurs 3 times.

In the second sample you don't need to perform a single operation or increase each element by one. If we do nothing, we get array 5, 5, 5, if we increase each by one, we get 6, 6, 6. In both cases the maximum number of occurrences equals 3. So we should do nothing, as number 5 is less than number 6.

In the third sample we should increase the second array element once and the fifth element once. Thus, we get sequence 3, 2, 2, 2, 2, where number 2 occurs 4 times.

二分区间或者截取区间

给个截取的吧

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>


typedef long long ll;
using namespace std;
ll a[100005];

ll sum[100005];

int main()
{
	int n,k;
	cin>>n>>k;
	for(int t=1;t<=n;t++)
	{
		scanf("%lld",&a[t]);
	}
	sort(a+1,a+n+1);
	for(int t=1;t<=n;t++)
	{
		sum[t]=sum[t-1]+a[t];
		
	}
	ll pos=0;
	ll maxn=0;
	ll ss; 
	for(int t=1;t<=n;t++)
	{
		while(t>pos&&(a[t]*(t-pos)-sum[t]+sum[pos]>k))
		{
			pos++;
		}
		
		if(t-pos>maxn)
		{
		    
			maxn=t-pos;
			ss=a[t];
		}
	}
	cout<<maxn<<" "<<ss<<endl;
	
	
	return 0;
}

J - Tempter of the Bone

The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze. 

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him. 

Input

The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following: 

'X': a block of wall, which the doggie cannot enter; 
'S': the start point of the doggie; 
'D': the Door; or 
'.': an empty block. 

The input is terminated with three 0's. This test case is not to be processed. 

Output

For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise. 

Sample Input

4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0

Sample Output

NO
YES

奇偶剪枝

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<set> 
#include<vector>
#include<cmath>

typedef long long ll;
using namespace std;
int flag,sx,sy,ex,ey,num;
int n,m,t,vis[10][10];
int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};
char map[10][10];
int abs(int p)
{
   return p>=0?p:-p;
}
void dfs(int x,int y,int sum)
{  
    int i,xx,yy;
    if(flag==1)
        return;
    if(x==ex&&y==ey&&sum==t)
    {
        flag=1;
        return;
    }
    int mindis=abs(x-ex)+abs(y-ey);  
    if(mindis>t-sum||(mindis+ t-sum )%2!=0)  
        return;
       
    for(i=0;i<4;i++)
    {
        xx=x+dx[i];
        yy=y+dy[i];
        if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy]&&map[xx][yy]!='X')  
        {
            vis[xx][yy]=1;
            dfs(xx,yy,sum+1);
            vis[xx][yy]=0;
        }
    }
}
int main()
{
    int i,j;
    while(~scanf("%d%d%d",&n,&m,&t))
    {
        if(n==0&&m==0&&t==0)
            break;
        num=0;
        for(i=0;i<n;i++)
        {
            scanf("%s",map[i]);
            for(j=0;j<m;j++)
            {
                if(map[i][j]=='S')
                {
                    sx=i;
                    sy=j; 
                }
                if(map[i][j]=='D')
                {
                    ex=i;
                    ey=j; 
                }
                if(map[i][j]=='X')
                    num++; 
            }
        }
        if(n*m-num-1<t) 
        {
            printf("NO
");
            continue;
        }
        flag = 0;
        memset(vis, 0, sizeof(vis));
        vis[sx][sy] = 1;
           dfs(sx, sy, 0);
        if(flag)
           printf("YES
");
        else
           printf("NO
");
    }
    return 0;
}

K - LLPS

This problem's actual name, "Lexicographically Largest Palindromic Subsequence" is too long to fit into the page headline.

You are given string s consisting of lowercase English letters only. Find its lexicographically largest palindromic subsequence.

We'll call a non-empty string s[p1p2... pk] = sp1sp2... spk (1  ≤  p1 < p2 < ... < pk  ≤ |s|) a subsequence of string s = s1s2... s|s|, where |s| is the length of string s. For example, strings "abcb", "b" and "abacaba" are subsequences of string "abacaba".

String x = x1x2... x|x| is lexicographically larger than string y = y1y2... y|y| if either |x| > |y| and x1 = y1, x2 = y2, ..., x|y| = y|y|, or there exists such number r (r < |x|, r < |y|) that x1 = y1, x2 = y2, ..., xr = yr and xr  +  1 > yr  +  1. Characters in the strings are compared according to their ASCII codes. For example, string "ranger" is lexicographically larger than string "racecar" and string "poster" is lexicographically larger than string "post".

String s = s1s2... s|s| is a palindrome if it matches string rev(s) = s|s|s|s| - 1... s1. In other words, a string is a palindrome if it reads the same way from left to right and from right to left. For example, palindromic strings are "racecar", "refer" and "z".

Input

The only input line contains a non-empty string s consisting of lowercase English letters only. Its length does not exceed 10.

Output

Print the lexicographically largest palindromic subsequence of string s.

Examples

Input

radar

Output

rr

Input

bowwowwow

Output

wwwww

Input

codeforces

Output

s

Input

mississipp

Output

ssss

Note

Among all distinct subsequences of string "radar" the following ones are palindromes: "a", "d", "r", "aa", "rr", "ada", "rar", "rdr", "raar" and "radar". The lexicographically largest of them is "rr".

大水题

不说了

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>

typedef long long ll;
using namespace std;
string str;
int vis[105];
int main()
{

	cin>>str;
	int len=str.length();
	for(int t=0;t<len;t++)
	{
	      vis[str[t]-'a']++;
	}
	int flag=0;
	for(int t=25;t>=0;t--)
	{
		while(vis[t]!=0)
		{
			for(int j=0;j<vis[t];j++)
			{
				char s='a'+t;
				cout<<s;
			}
			vis[t]=0;
			flag=1;
		}
		if(flag)
		{
			break;
		}
	}
	
	return 0;
}

 

  

原文地址:https://www.cnblogs.com/Staceyacm/p/10781788.html