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\n",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\n");
continue;
}
flag = 0;
memset(vis, 0, sizeof(vis));
vis[sx][sy] = 1;
dfs(sx, sy, 0);
if(flag)
printf("YES\n");
else
printf("NO\n");
}
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;
}

QDU_AP协会18级ST1的更多相关文章

  1. 复旦高等代数II(18级)每周一题

    本学期将继续进行高等代数每周一题的活动.计划从第一教学周开始,到第十五教学周结束,每周的周末公布一道思考题(预计15道),供大家思考和解答.每周一题将通过“高等代数官方博客”(以博文的形式)和“高等代 ...

  2. 复旦大学2018--2019学年第一学期(18级)高等代数I期末考试第七大题解答

    七.(本题10分)  设 $V$ 为 $n$ 维线性空间, $\varphi,\psi$ 是 $V$ 上的线性变换, 满足 $\varphi\psi=\varphi$. 证明: $\mathrm{Ke ...

  3. ArcMap 制作广州 18 级地图切片需要多少时间?

    制作地图切片包会随着级别的上升,瓦片数量会指数级地上升,所需的计算时间也是指数级的. 但是 ArcMap 并不会提示时间信息,只有一个圈没完没了地转... 就在这无聊地等待中,我写了这篇帖子. 电脑配 ...

  4. 18级北航软件学院算法复习--Samshui

    A 比特手链 简单模拟 判断 贪心 叶姐要想哥赠送一串比特手链,这个手链由0和1组成.想哥买了手链B,无意间得知叶姐想要同样长度的手链A.想哥囊中羞涩,只能手工调整手链.他希望最少通过以下操作进行最少 ...

  5. 复旦高等代数 I(18级)每周一题

    [问题2018A01]  计算下列 $n+1$ 阶行列式的值: $$|A|=\begin{vmatrix} 0 & 1 & 1 & \cdots & 1 \\ 1 &a ...

  6. 网络工程18级《C++程序设计II》实践作业1

    A.类的应用1 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 162 (133 users) Total Accepted: 136 ...

  7. 复旦大学2018--2019学年第二学期(18级)高等代数II期末考试第六大题解答

    六.(本题10分)  设 $A$ 为 $n$ 阶实对称阵, 证明: $A$ 有 $n$ 个不同的特征值当且仅当对 $A$ 的任一特征值 $\lambda_0$ 及对应的特征向量 $\alpha$, 矩 ...

  8. 2019华东交通大学ACM基地简介

    一.基地成就简介: ACM国际大学生程序设计竞赛(英文全称:ACM International Collegiate Programming Contest(简称ACM-ICPC或ICPC))是由国际 ...

  9. SQL Server存储过程

    创建于2016-12-24 16:12:19 存储过程 概念: 1.存储过程是在数据库管理系统中保存的.预先编译的.能实现某种功能的SQL程序,它是数据库应用中运用比较广泛的 一种数据对象. 2.存储 ...

随机推荐

  1. servlet01 项目demo、servlet生命周期

    1 环境说明 jdk: 1.8 tomcat: 8.0 2 项目demo 2.1 新建一个动态的web项目   2.2 新建一个servlet类 该类必须继承 HttpServlet 技巧01:Htt ...

  2. 项目代码:js

    1 //获取发文时间 function selectWriteTime(){ $("#writing_time_index").on("click"," ...

  3. orzdba工具安装注意事项

    orzdba是一个监控mysql性能的一个比较好用的perl脚本,是淘宝开源的小工具,下载地址http://code.taobao.org/p/orzdba/src/trunk/ 配置过程中除了参照& ...

  4. 什么是Kali Linux?

    什么是Kali Linux? Kali Linux是一个基于Debian的Linux发行版,旨在实现高级渗透测试和安全审计.Kali包含数百种工具,适用于各种信息安全任务,如渗透测试,安全研究,计算机 ...

  5. Docker学习笔记_安装和使用Apache

    一.准备 1.宿主机OS:Win10 64位 2.虚拟机OS:Ubuntu18.04 3.账号:docker 二.安装 1.搜索镜像                                  ...

  6. mybatis的执行流程 #{}和${} Mysql自增主键返回 resultMap 一对多 多对一配置

    n Mybatis配置 全局配置文件SqlMapConfig.xml,配置了Mybatis的运行环境等信息. Mapper.xml文件即Sql映射文件,文件中配置了操作数据库的Sql语句.此文件需要在 ...

  7. 修改QPushButton北京颜色和字体背景

    项目需要修改按钮背景的颜色 QPalette pal = startBtn.palette(); //startBtn是我已经定义好的QPushButton对象 pal.setColor(QPalet ...

  8. 【原创】请不要对Boost Format使用Byte作为参数

    曾几何时我们可以肆无忌惮的对sprintf传入BYTE等类型作为参数,只要你指定的为%D即可打印出对应的数字 但是boost format不可以,当你发生类型截断,错误,异常,请尽快查看你传入的类型是 ...

  9. linux 的各个文件夹都是干什么用

    http://www.ruanyifeng.com/blog/2012/02/a_history_of_unix_directory_structure.html http://www.pathnam ...

  10. 【Android学习】Android编码规范

    四种常见的命名法 比较Java和c#的命名规范的不同点 常量用大写 java方法首字母不大写,应该小写 函数行数限制 不要用拼音 参照物,Android源码 看源码工具,SourceInsight 和 ...