浙江财经大学
信息管理与工程学院

Codeforces B. African Crossword

本文由 Ocrosoft 于 2016-10-21 10:25:53 发表

An African crossword is a rectangular table n × m in size. Each cell of the table contains exactly one letter. This table (it is also referred to as grid) contains some encrypted word that needs to be decoded.

To solve the crossword you should cross out all repeated letters in rows and columns. In other words, a letter should only be crossed out if and only if the corresponding column or row contains at least one more letter that is exactly the same. Besides, all such letters are crossed out simultaneously.

When all repeated letters have been crossed out, we should write the remaining letters in a string. The letters that occupy a higher position follow before the letters that occupy a lower position. If the letters are located in one row, then the letter to the left goes first. The resulting word is the answer to the problem.

You are suggested to solve an African crossword and print the word encrypted there.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 100). Next n lines contain m lowercase Latin letters each. That is the crossword grid.

Output

Print the encrypted word on a single line. It is guaranteed that the answer consists of at least one letter.

Examples
input
3 3
cba
bcd
cbc

output
abcd

input
5 5
fcofd
ooedo
afaoa
rdcdf
eofsf

output
codeforces

Solution

题意:给一个n*m的字母矩阵,如果一个字母在其这一行/列中不止一个,就将其划去。最后剩下的字母按顺序组成答案。
思路:对每个字母判断行列是否有相同的。暴力可过。

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cctype>
#include <climits>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
#define strend string::npos
#define ms(a) memset(a,0,sizeof(a))
typedef long long LL;
const LL LINF = LLONG_MAX / 2;
const int INF = INT_MAX / 2;
const int MAXN = 150 + 10;
const int MOD = 100000;
int gcd(int a, int b)
{
	if (!b)return a;
	return gcd(b, a%b);
}
using namespace std;
char mp[120][120];
bool vis[120][120];
int main()
{
	int n,m;
	string ans="";
	cin>>n>>m;
	for(int i=0;i<n;i++)cin>>mp[i];
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			char c=mp[i][j];
			for(int k=i+1;k<n;k++)
				if(mp[k][j]==c)
					vis[i][j]=vis[k][j]=1;
			for(int k=j+1;k<m;k++)
				if(mp[i][k]==c)
					vis[i][j]=vis[i][k]=1;
		}
	}		
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			if(!vis[i][j])ans.push_back(mp[i][j]);
	cout<<ans<<endl;
	return 0;
}

欢迎分享与转载,请保留链接与出处。Ocrosoft » Codeforces B. African Crossword

点赞 (0)or拍砖 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址