Gym 101078

A題

比較兩個由數字1~n組成的序列,輸出每段最短的相似的序列。(相似代表這段中兩個序列包含的數字相同,順序可以不同)

Input
2
10
1 2 3 6 4 7 5 8 9 10
3 2 1 4 5 6 7 8 10 9
5
2 1 4 5 3
2 4 5 3 1
Output
1-3 4-7 8-8 9-10
1-1 2-5


用一個hash數組來標記某數字出現的次數,每掃描到一個數字就把它的出現次數加一。如果掃描到的數字出現次數爲0,就把數字總數加1;如果爲1,就把數字總數減1. 若總數爲0,說明所有數字都出現了2次,輸出當前區間即可。

用變量s記錄區間的位置。

#include<iostream>
#include<cstdio>
using namespace std;

int a[100010],b[100010],num[100010];
int main()
{
	int _,n,sum;

	scanf("%d",&_);
	while (_--){
		scanf("%d",&n);
		for (int i=0; i<n; i++) scanf("%d",&a[i]);
		for (int i=0; i<n; i++) scanf("%d",&b[i]);
		sum=0;
		int s=1,r=0;
		for (int i=1; i<=n; i++) num[i]=0;
		for (int i=0; i<n; i++){
			int t=a[i];
			if (!num[t]){
				sum++;
			}
			else {
				sum--;
			}
			num[t]++;
			t=b[i];
			if (!num[t]){
				sum++;
			}
			else {
				sum--;
			}
			num[t]++;
			if (sum==0){
				if (r) printf(" %d-%d",s,i+1);
				else printf("%d-%d",s,i+1);
				r++;
				s=i+2;
			}
		}
		printf("\n");
	}


	return 0;
}

也可以用優先隊列記錄倆個序列裏的值,每加入一個就把它們中的相同元素去掉,直到兩個優先隊列都爲空爲止。

D題

Collatz
Problem
In the process to solve the Collatz conjecture, better known as the 3n + 1 problem, Carl created a
physical model with wood and ropes. A wooden bar contains a hole for every natural number from
1 to infinity from left to right. For every even number m there is a rope connecting the mth hole
with hole m/2 . For every odd number n there is a rope connecting the nth hole with hole 3n + 1.
For an important conference where Carl plans to elaborate on his results, he wants to bring
his structure, but it is too large to fit in his bag. So he decided to saw o the part of the bar
containing the first N holes only. How many ropes will he need to cut?
Input
The first line of the input contains a single number: the number of test cases to follow. Each test
case has the following format:
• One line with an integer N, satisfying 0 ≤ N ≤
109 .
Output
For every test case in the input, the output should contain a single number, on a single line: the
number of ropes that need to be cut.
Example
Input
3
12
240
3600
Output
10
200
3000

手算出來奇數情況和偶數情況下需要割斷的繩子數即可。

#include<iostream>
#include<cstdio>
using namespace std;

int main()
{
	int _,n,ans;

	scanf("%d",&_);
	while (_--){
		scanf("%d",&n);
		ans=0;
		if (n&1)
		{
			ans+=n-n/2;
			ans+=(n-(n-1)/3+1)/2;
		}
		else{
			ans+=n/2;
			ans+=(n-(n-1)/3)/2;
		}
		printf("%d\n",ans);
	}



	return 0;
}

I題

I Keylogger
Problem
As a malicious hacker you are trying to steal your mother’s password, and therefore you have
installed a keylogger on her PC (or Mac, so you like). You have a log from your mother typing
the password, but unfortunately the password is not directly visible because she used the left and
right arrows to change the position of the cursor, and the backspace to delete some characters.
Write a program that can decode the password from the given keylog.
Input
The first line of the input contains a single number: the number of test cases to follow. Each test
case has the following format:
• One line with a string L, satisfying 1 ≤ Length(L) ≤ 1, 000, 000, consisting of:
– ’-’ representing backspace: the character directly before the cursor position is deleted,
if there is any.
– ’<’(and ’>’) representing the left (right) arrow: the cursor is moved 1 character to
the left (right), if possible.
– alphanumeric characters, which are part of the password, unless deleted later. We
assume ‘insert mode’: if the cursor is not at the end of the line, and you type an
alphanumeric character, then all characters after the cursor move one position to the
right.
Every decoded password will be of length > 0.
Output
For every test case in the input, the output should contain a single string, on a single line: the
decoded password.
Example
Input
2
<<BP<A>>Cd-
ThIsIsS3Cr3t
Output
BAPC
ThIsIsS3Cr3t
This page intentionally left blank.

鏈表模擬題

J題

題目大意:

    有t組輸入 ,接下來兩行,表示橫着的字符串有多少個,豎着的字符串有多少個,然後接下來那些行每行三個元素,表示起點座標,和字符串。問最多能夠在方格紙上寫下多少字符串,並且使其都沒有矛盾。

    題目保證只有橫豎的字符串會有交點。

思路:

    橫着和豎着的單詞看成兩類點,衝突看成邊,求出最大獨立集就可以得到兩兩間沒有衝突的單詞數。

    最大獨立集=頂點個數 – 最小頂點覆蓋(最大匹配)

 

#include <iostream>
#include <string.h>
#include <cstdio>
using namespace std;
int e[1051][1001];
int match[1110];
int book[1100];
int n,m;
int dfs(int u)
{
    for(int i=1;i<=m;i++)
    {
        if(book[i]==0&&e[u][i]==1)
        {
            book[i]=1; //標記點i已訪問過
            if(match[i]==0||dfs(match[i]))
            {
                //更新配對關係
                match[i]=u;
                return 1;//把dfs(u)置爲true
            }
        }
    }
    return 0;
}

int xh[550],yh[550],xv[550],yv[550];
   char h[550][1010],v[550][1010];
int main()
{
   int _;
   scanf("%d",&_);
   while (_--){
 	
   scanf("%d%d",&n,&m);
 	
   for(int i=1;i<=n;i++){
    	scanf("%d%d",&xh[i],&yh[i]);
    	scanf("%s",h[i]);
   }
   for(int i=1;i<=m;i++){
    	scanf("%d%d",&xv[i],&yv[i]);
    	scanf("%s",v[i]);
   }
   for (int i=1; i<=n+m; i++)
   	for (int j=1; j<=n+m; j++)
   		e[i][j]=0;
// int pp=0;

//建邊
   for (int i=1; i<=n; i++){
	   	for (int j=1; j<=m; j++){
	   		if ((yh[i]<yv[j]) || (yh[i]>=yv[j]+(int)strlen(v[j]))) continue;
	   		if ((xv[j]<xh[i]) || (xv[j]>=xh[i]+(int)strlen(h[i]))) continue;
	   		if (h[i][xv[j]-xh[i]]!=v[j][yh[i]-yv[j]]){
		   		e[i][j]=1;
		   		// printf("i,j:%d %d\n",i,j );
		   		// pp++;
		   	}
		}
   	}
	// printf("ppp: %d\n",pp );
	// n+=m;
	for(int i=1;i<=m;i++)
    	match[i]=0;
 	
 	int sum=0;

	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++)
        	book[j]=0;           //清空上次搜索時的標記
    	if(dfs(i))
        	sum++;           
        	   //尋找增廣路,如果找到,配對數加1.
   	}

	printf("%d\n",n+m-sum);}
    return 0;
}
/*
2
2 2
0 1 BAPC
0 2 LEIDEN
0 0 SOLUTION
2 1 WINNER
1 4
0 1 HELLO
1 0 HI
2 0 BYE
3 0 GOODBYE
4 0 FAREWELL
*/

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章