A题:http://codeforces.com/contest/510/problem/A
Fox Ciel starts to learn programming. The first task is drawing a fox! However, that turns out to be too hard for a beginner, so she decides to draw a snake instead.
A snake is a pattern on a n by m table. Denote c-th cell of r-th row as (r, c). The tail of the snake is located at (1, 1), then it's body extends to (1, m), then goes down 2 rows to (3, m), then goes left to (3, 1) and so on.
Your task is to draw this snake for Fox Ciel: the empty cells should be represented as dot characters ('.') and the snake cells should be filled with number signs ('#').
Consider sample tests in order to understand the snake pattern.
The only line contains two integers: n and m (3 ≤ n, m ≤ 50).
n is an odd number.
Output n lines. Each line should contain a string consisting of m characters. Do not output spaces.
3 3
### ..# ###
3 4
#### ...# ####
5 3
### ..# ### #.. ###
9 9
######### ........# ######### #........ ######### ........# ######### #........ #########
代码:
#include<iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <string.h>
#include <iomanip>
#include <queue>
#include <stack>
#include<map>
using namespace std;
typedef long long ll;
int n,m,k;
const int N=310;
char arr[N][N];
int main()
{
while(cin>>n>>m)
{
for(int i=1;i<=n;i++)
{
if(i&1)
{
for(int j=1;j<=m;j++)
cout<<"#";
cout<<endl;
}
else
{
int t = i/2;
if(t&1)
{
for(int j=1;j<=m-1;j++)
cout<<".";
cout<<"#"<<endl;
}
else
{
cout<<"#";
for(int j=1;j<=m-1;j++)
cout<<".";
cout<<endl;
}
}
}
}
return 0;
}
B题: http://codeforces.com/contest/510/problem/B
Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n × m cells, like this:
Each cell contains a dot that has some color. We will use different uppercase Latin characters to express different colors.
The key of this game is to find a cycle that contain dots of same color. Consider 4 blue dots on the picture forming a circle as an example. Formally, we call a sequence of dots d1, d2, ..., dk a cycle if and only if it meets the following condition:
- These k dots are different: if i ≠ j then di is different from dj.
- k is at least 4.
- All dots belong to the same color.
- For all 1 ≤ i ≤ k - 1: di and di + 1 are adjacent. Also, dk and d1 should also be adjacent. Cells x and y are called adjacent if they share an edge.
Determine if there exists a cycle on the field.
The first line contains two integers n and m (2 ≤ n, m ≤ 50): the number of rows and columns of the board.
Then n lines follow, each line contains a string consisting of m characters, expressing colors of dots in each line. Each character is an uppercase Latin letter.
Output "Yes" if there exists a cycle, and "No" otherwise.
3 4 AAAA ABCA AAAA
Yes
3 4 AAAA ABCA AADA
No
4 4 YYYR BYBY BBBY BBBY
Yes
7 6 AAAAAB ABBBAB ABAAAB ABABBB ABAAAB ABBBAB AAAAAB
Yes
2 13 ABCDEFGHIJKLM NOPQRSTUVWXYZ
No
In first sample test all 'A' form a cycle.
In second sample there is no such cycle.
The third sample is displayed on the picture above ('Y' = Yellow, 'B' = Blue, 'R' = Red).
分析:题目意思是要我们判断有没有这样的一个圈,圈上颜色都是一样的,那么其实就是一个搜索题,这里我采用dfs(),要是一直根据相同颜色dfs下去,如果碰到了访问过的某个节点,那么就说明必然有相同颜色的一个圈存在,要注意的是,在dfs()过程中,下一步是不能再回到上一步的那个节点的。
代码:
#include<iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <string.h>
#include <iomanip>
#include <queue>
#include <stack>
#include<map>
using namespace std;
typedef long long ll;
int n,m,k;
const int N=310;
char arr[N][N];
bool flag = false;
bool visit[N][N];
int dirx[]={1,0,-1,0};
int diry[]={0,1,0,-1};
void dfs(int i,int j,int pi,int pj)
{
if(visit[i][j])
{
flag = true;
return;
}
visit[i][j]=true;
for(int k=0;k<4;k++)
{
int nextx = i+dirx[k];
int nexty = j+diry[k];
if(nextx>=0&&nextx<n&&nexty>=0&&nexty<m&&arr[i][j]==arr[nextx][nexty])
{
if(!(nextx==pi&&nexty==pj))
dfs(nextx,nexty,i,j);
}
}
}
int main()
{
while(cin>>n>>m)
{
flag = false;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cin>>arr[i][j];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
memset(visit,0,sizeof(visit));
dfs(i,j,-1,-1);
}
if(flag)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}
C题: http://codeforces.com/contest/510/problem/C
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.
After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted inlexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!
She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.
Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters:si ≠ ti. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters siand ti according to their order in alphabet.
The first line contains an integer n (1 ≤ n ≤ 100): number of names.
Each of the following n lines contain one string namei (1 ≤ |namei| ≤ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'–'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).
Otherwise output a single word "Impossible" (without quotes).
3 rivest shamir adleman
bcdefghijklmnopqrsatuvwxyz
10 tourist petr wjmzbmr yeputons vepifanov scottwu oooooooooooooooo subscriber rowdark tankengineer
Impossible
10 petr egor endagorion feferivan ilovetanyaromanova kostka dmitriyh maratsnowbear bredorjaguarturnik cgyforever
aghjlnopefikdmbcqrstuvwxyz
7 car care careful carefully becarefuldontforgetsomething otherwiseyouwillbehacked goodluck
acbdefhijklmnogpqrstuvwxyz
代码:
#include<iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <string.h>
#include <iomanip>
#include <queue>
#include <stack>
using namespace std;
typedef long long ll;
int n,k;
const int N=110;
string arr[N];
int indegree[N];
bool visited[N];
vector<int> edge[N];
int m,r,t;
void topsort()
{
vector<int> ret;
for(int i=0;i<26;i++)
{
if(indegree[i]==0&&edge[i].size()==0)
{
ret.push_back(i);
visited[i]=true;
}
}
while(true)
{
int i = 0;
for(;i<26;i++)
{
if(indegree[i]==0&&!visited[i])
{
break;
}
}
if(i<26)
{
ret.push_back(i);
for(int j=0;j<edge[i].size();j++)
{
int t = edge[i][j];
indegree[t]--;
}
edge[i].clear();
visited[i]=true;
}
else
{
bool flag = false;
for(int i=0;i<26;i++)
{
if(visited[i]==false)
flag = true;
}
if(flag)
{
cout<<"Impossible"<<endl;
return;
}
else
break;
}
}
for(int i=0;i<ret.size();i++)
{
char c = ret[i]+'a';
cout<<c;
}
cout<<endl;
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
cin>>arr[i];
memset(visited,0,sizeof(visited));
for(int i=1;i<n;i++)
{
string sa = arr[i-1];
string sb = arr[i];
int a = -1;
int b = -1;
for(int j=0;j<min(sa.length(),sb.length());j++)
{
if(sa[j]==sb[j])
continue;
else
{
a = sa[j]-'a';
b = sb[j]-'a';
break;
}
}
if(a==-1)
{
if(sa.length()>sb.length())
{
cout<<"Impossible"<<endl;
return 0;
}
else
continue;
}
indegree[b]++;
edge[a].push_back(b);
}
topsort();
return 0;
}
Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0.
There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li).
She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible.
If this is possible, calculate the minimal cost.
The first line contains an integer n (1 ≤ n ≤ 300), number of cards.
The second line contains n numbers li (1 ≤ li ≤ 109), the jump lengths of cards.
The third line contains n numbers ci (1 ≤ ci ≤ 105), the costs of cards.
If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards.
3 100 99 9900 1 1 1
2
5 10 20 30 40 50 1 1 1 1 1
-1
7 15015 10010 6006 4290 2730 2310 1 1 1 1 1 1 1 10
6
8 4264 4921 6321 6984 2316 8432 6120 1026 4264 4921 6321 6984 2316 8432 6120 1026
7237
In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell.
In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1.
代码:
#include<iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <string.h>
#include <iomanip>
#include <queue>
#include <stack>
#include<map>
using namespace std;
typedef long long ll;
int n,k;
const int N=310;
int l[N];
int c[N];
vector<int> edge[N];
int m,r,t;
map<int,int> dp;
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
cin>>l[i];
for(int i=0;i<n;i++)
cin>>c[i];
dp[0]=0;
for(int i=0;i<n;i++)
{
map<int,int>::iterator it = dp.begin();
for(;it!=dp.end();it++)
{
int t = gcd(l[i],it->first);
if(dp.count(t))
dp[t] = min(dp[t],it->second+c[i]);
else
dp[t] = it->second+c[i];
}
}
if(dp.count(1))
cout<<dp[1]<<endl;
else
cout<<-1<<endl;
return 0;
}