A. Hongcow Learns the Cyclic Shift
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Hongcow is learning to spell! One day, his teacher gives him a word that he needs to learn to spell. Being a dutiful student, he immediately learns how to spell the word.

Hongcow has decided to try to make new words from this one. He starts by taking the word he just learned how to spell, and moves the last character of the word to the beginning of the word. He calls this a cyclic shift. He can apply cyclic shift many times. For example, consecutively applying cyclic shift operation to the word "abracadabra" Hongcow will get words "aabracadabr", "raabracadab" and so on.

Hongcow is now wondering how many distinct words he can generate by doing the cyclic shift arbitrarily many times. The initial string is also counted.

Input

The first line of input will be a single string s (1 ≤ |s| ≤ 50), the word Hongcow initially learns how to spell. The string s consists only of lowercase English letters ('a'–'z').

Output

Output a single integer equal to the number of distinct strings that Hongcow can obtain by applying the cyclic shift arbitrarily many times to the given string.

Examples
input
abcd
output
4
input
bbb
output
1
input
yzyz
output
2
Note

For the first sample, the strings Hongcow can generate are "abcd", "dabc", "cdab", and "bcda".

For the second sample, no matter how many times Hongcow does the cyclic shift, Hongcow can only generate "bbb".

For the third sample, the two strings Hongcow can generate are "yzyz" and "zyzy".

题意:给你一个字符串,字符串可以将最后一个放第一个,其余全部往后推,问最多有多少个不同的字符串;

思路:暴力枚举起始点,字符串hash;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=1e5+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=;
char a[N];
map<string,int>m;
int main()
{
scanf("%s",&a);
int n=strlen(a);
int ans=;
for(int i=;i<n;i++)
{
string aa="";
for(int j=i;j<n;j++)
aa+=a[j];
for(int j=;j<i;j++)
aa+=a[j];
if(!m[aa])
ans++;
m[aa]=;
}
printf("%d\n",ans);
return ;
}
B. Hongcow Solves A Puzzle
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Hongcow likes solving puzzles.

One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.

The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap.

You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position.

Input

The first line of input will contain two integers n and m (1 ≤ n, m ≤ 500), the dimensions of the puzzle piece.

The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space.

It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region.

Output

Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise.

Examples
input
2 3
XXX
XXX
output
YES
input
2 2
.X
XX
output
NO
input
5 5
.....
..X..
.....
.....
.....
output
YES
Note

For the first sample, one example of a rectangle we can form is as follows

111222
111222

For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.

In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle:

.....
..XX.
.....
.....
.....

题意:问你是否只有一个矩形;

思路:找到最上面左边的点和最下面右边为矩阵左右边界,判断是否问矩形,再求矩形外面是否有X;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=1e3+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=;
char mp[N][N];
int stx,sty;
int enx,eny;
int n,m;
void checks()
{
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(mp[i][j]=='X')
{
stx=i;
sty=j;
return;
}
}
}
}
void checke()
{
for(int i=n;i>=;i--)
{
for(int j=m;j>=;j--)
{
if(mp[i][j]=='X')
{
enx=i;
eny=j;
return;
}
}
}
}
int check(int s,int ss,int e,int ee)
{
int sum=;
for(int i=s;i<=e;i++)
{
for(int j=ss;j<=ee;j++)
{
if(mp[i][j]!='X')
return -;
sum++;
}
}
return sum;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%s",mp[i]+);
}
checks();
checke();
if(!stx||!enx)
return puts("NO\n");
int no=check(stx,sty,enx,eny),ans=;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
if(mp[i][j]=='X')
ans++;
}
if(ans==no)
printf("YES\n");
else
printf("NO\n");
return ;
}
C. Hongcow Builds A Nation
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Hongcow is ruler of the world. As ruler of the world, he wants to make it easier for people to travel by road within their own countries.

The world can be modeled as an undirected graph with n nodes and m edges. k of the nodes are home to the governments of the kcountries that make up the world.

There is at most one edge connecting any two nodes and no edge connects a node to itself. Furthermore, for any two nodes corresponding to governments, there is no path between those two nodes. Any graph that satisfies all of these conditions is stable.

Hongcow wants to add as many edges as possible to the graph while keeping it stable. Determine the maximum number of edges Hongcow can add.

Input

The first line of input will contain three integers nm and k (1 ≤ n ≤ 1 000, 0 ≤ m ≤ 100 000, 1 ≤ k ≤ n) — the number of vertices and edges in the graph, and the number of vertices that are homes of the government.

The next line of input will contain k integers c1, c2, ..., ck (1 ≤ ci ≤ n). These integers will be pairwise distinct and denote the nodes that are home to the governments in this world.

The following m lines of input will contain two integers ui and vi (1 ≤ ui, vi ≤ n). This denotes an undirected edge between nodes ui andvi.

It is guaranteed that the graph described by the input is stable.

Output

Output a single integer, the maximum number of edges Hongcow can add to the graph while keeping it stable.

Examples
input
4 1 2
1 3
1 2
output
2
input
3 3 1
2
1 2
1 3
2 3
output
0
Note

For the first sample test, the graph looks like this:

Vertices 1 and 3 are special. The optimal solution is to connect vertex 4 to vertices 1 and 2. This adds a total of 2 edges. We cannot add any more edges, since vertices 1 and 3 cannot have any path between them.

For the second sample test, the graph looks like this:

We cannot add any more edges to this graph. Note that we are not allowed to add self-loops, and the graph must be simple.

题意:给你一个无向图,n个村庄,m条边,k个政府,不能让两个政府联通,问最多能加多少条边;

思路:首先并查集一下,找到一个最大的集合,将无政府的点,加进去,然后将每个集合所有点有相互连接即可;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=1e5+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=;
int fa[N],si[N];
int flag[N];
int Find(int x)
{
return x==fa[x]?x:fa[x]=Find(fa[x]);
}
void update(int x,int y)
{
int u=Find(x);
int v=Find(y);
if(u!=v)
{
if(flag[u])
{
fa[v]=u;
si[u]+=si[v];
}
else
{
fa[u]=v;
si[v]+=si[u];
}
}
}
int u[N],v[N];
int main()
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=k;i++)
{
int x;
scanf("%d",&x);
flag[x]=;
}
for(int i=;i<=n;i++)
{
fa[i]=i;
si[i]=;
}
for(int i=;i<=m;i++)
{
scanf("%d%d",&u[i],&v[i]);
update(u[i],v[i]);
}
int ma=,pos;
for(int i=;i<=n;i++)
{
if(flag[i])
{
if(si[i]>ma)
{
ma=si[i];
pos=i;
}
}
}
int ans=;
for(int i=;i<=n;i++)
{
int x=Find(i);
if(x==pos||!flag[x])
ans++;
}
//cout<<ans<<endl;
ans=ans*(ans-)/;
for(int i=;i<=n;i++)
{
int x=Find(i);
if(x!=pos&&flag[x])
ans+=si[x]*(si[x]-)/;
flag[x]=;
}
ans-=m;
printf("%d\n",ans);
return ;
}

Codeforces Round #385 (Div. 2) A,B,C 暴力,模拟,并查集的更多相关文章

  1. Codeforces Round #376 (Div. 2) A B C 水 模拟 并查集

    A. Night at the Museum time limit per test 1 second memory limit per test 256 megabytes input standa ...

  2. Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary 并查集

    D. Mahmoud and a Dictionary 题目连接: http://codeforces.com/contest/766/problem/D Description Mahmoud wa ...

  3. Codeforces Round #250 (Div. 1) B. The Child and Zoo 并查集

    B. The Child and Zoo Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/438/ ...

  4. Codeforces Round #212 (Div. 2) D. Fools and Foolproof Roads 并查集+优先队列

    D. Fools and Foolproof Roads   You must have heard all about the Foolland on your Geography lessons. ...

  5. Codeforces Round #254 (Div. 2) B. DZY Loves Chemistry (并查集)

    题目链接 昨天晚上没有做出来,刚看题目的时候还把题意理解错了,当时想着以什么样的顺序倒,想着就饶进去了, 也被题目下面的示例分析给误导了. 题意: 有1-n种化学药剂  总共有m对试剂能反应,按不同的 ...

  6. Codeforces Round #260 (Div. 1) C. Civilization 树的中心+并查集

    题目链接: 题目 C. Civilization time limit per test1 second memory limit per test256 megabytes inputstandar ...

  7. Codeforces Round #164 (Div. 2) A. Games【暴力/模拟/每个球队分主场和客场,所有球队两两之间进行一场比赛,要求双方球服颜色不能相同】

    A. Games time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  8. Codeforces Round #250 (Div. 2) D. The Child and Zoo 并查集

    D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. Codeforces Round #329 (Div. 2) D. Happy Tree Party(LCA+并查集)

    题目链接 题意:就是给你一颗这样的树,用一个$y$来除以两点之间每条边的权值,比如$3->7$,问最后的y的是多少,修改操作是把权值变成更小的. 这个$(y<=10^{18})$除的权值如 ...

随机推荐

  1. 基于C#和Asp.NET MVC开发GPS部标监控平台

    基于交通部796标准开发部标监控平台,选择开发语言和技术也是团队要思考的因素,其实这由团队自己擅长的技术来决定,如果擅长C#和Asp.NET, 当然开发效率就高很多.当然了技术选型一定要选用当前主流的 ...

  2. PostgreSQL 磁盘使用大小监控

    表大小信息 postgres=# SELECT *, pg_size_pretty(total_bytes) AS totalpostgres-# , pg_size_pretty(index_byt ...

  3. 【皇甫】☀Struts_第一节课

    本章讲解内容: DTD是Docunent Type Defintion的缩写,即文档类型定义.DTD用来描述XML文档结构. DOM4J是一个非常优秀的javaXML API,具有性能优异,功能强大和 ...

  4. android案例一 电话拨号器

    效果图:   电话拨号器的核心原理:  意图   MainActivity代码:     private EditTest et_number; //加载一个布局 setContentView(R.l ...

  5. 【MongoDB for Java】Java操作MongoDB

    上一篇文章: http://www.cnblogs.com/hoojo/archive/2011/06/01/2066426.html介绍到了在MongoDB的控制台完成MongoDB的数据操作,通过 ...

  6. Coursera台大机器学习课程笔记7 -- Noise and Error

    本章重点:  简单的论证了即使有Noise,机器依然可以学习,VC Dimension对泛化依然起作用:介绍了一些评价Model效果的Error Measurement方法. 一论证即使有Noisy, ...

  7. Vs2012编写C语言

    本来我也是用VC++6.0编写C语言的,但是由于这个版本过老并且和win7,win8,win10的不兼容所以去下载了vs2012,一开始的时候不知道怎么用,现在学会了就写一个教程分享一下. 这时就遇到 ...

  8. Magento-找出没有图片的产品

    最近维护网站,发现网站的产品很多都没有图片显示,看了一下是因为没有在后台勾选图片,就是 image small_image  thumbnail 这三项,就算有图片如果没有勾选的话也不会显示出来,产品 ...

  9. WebService "因 URL 意外地以 结束,请求格式无法识别" 的解决方法

    最近在做一个图片上传的功能,js调用用webservice进行异步访问服务器,对于不是经常用webservice的菜鸟来说,经常会遇到以下的问题(起码我是遇到了) 在页面上写了js调用代码如下所示: ...

  10. MS CRM 2013 Plugin 注册工具登录后空白

    解决办法 把en-us, zh-CN 目录随便改个名字就好了