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".

题意:给你一个字符串 每次可以把最尾部的一个字符放到最前面 问形成的字符串有多少种?

题解:模拟

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
#define bug() printf("!!!!!!!")
#define M 100005
using namespace std;
char s[];
map<string,int>mp;
int main()
{
scanf("%s",s);
string str;
str.assign(s);
mp[s]=;
int ans=;
int len=strlen(s);
int last=len-;
len--;
while(len)
{
len--;
char exm=s[last];
for(int i=last;i>=;i--)
{
s[i]=s[i-];
}
s[]=exm;
str.assign(s);
if(mp[str]==)
{
mp[str]++;
ans++;
}
}
cout<<ans<<endl;
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.
.....
.....
..... 题意:给你一个n*m的矩阵 由‘X’形成一个图形 问你是否可以用两个相同的图形拼成一个矩形(不可以旋转和翻折)
题解:确定给你的图形是否为一个矩形即可
 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
#define bug() printf("!!!!!!!")
#define M 100005
using namespace std;
int n,m;
char a[][];
int main()
{
scanf("%d %d",&n,&m);
getchar();
for(int i=;i<n;i++)
scanf("%s",a[i]);
int l=m-,r=,u=n-,d=;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(a[i][j]=='X')
{
l=min(l,j);
r=max(r,j);
u=min(u,i);
d=max(d,i);
}
}
}
for(int i=u;i<=d;i++)
{
for(int j=l;j<=r;j++)
{
if(a[i][j]=='.')
{
printf("NO\n");
return ;
}
}
}
printf("YES\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 k countries 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 n, m 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 and vi.

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个特殊点  要求k个点(我称为奇异点)互相不联通(已知的m条边不会使得k个点联通)  问你最多添加多少条边
题解:并查集找到各个联通块  在k个点中找到最大的联通块 与其他无奇异点的联通块中的点连边 各个联通块中补边到完全图  各个非奇异点间补边
 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
#define bug() printf("!!!!!!!")
#define M 100005
using namespace std;
int n,m,k;
int a[];
int fa[];
int l,r;
map<int,int>mp;
map<int,int>mpp;
int find(int root)
{
if(root!=fa[root])
return fa[root]=find(fa[root]);
else
return root;
}
void unio(int a,int b)
{
int aa=find(a);
int bb=find(b);
if(aa!=bb)
{
fa[aa]=bb;
}
}
int main()
{
scanf("%d %d %d",&n,&m,&k);
for(int i=;i<=k;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
fa[i]=i;
for(int i=;i<=m;i++)
{
scanf("%d %d",&l,&r);
unio(l,r);
}
for(int i=;i<=n;i++)
{
int x=find(i);
mp[fa[i]]++;
}
int maxn=;
int sum=;
int re=;
for(int i=;i<=k;i++)
{
maxn=max(maxn,mp[fa[a[i]]]);
sum+=mp[fa[a[i]]];
mpp[fa[a[i]]]=;
re=re+ mp[fa[a[i]]]*(mp[fa[a[i]]]-)/;
}
int b[];
int jishu=;
int ans=;
for(int i=;i<=n;i++)
{
if(mpp[fa[i]]==)
{
b[jishu++]=mp[fa[i]];
re=re+mp[fa[i]]*(mp[fa[i]]-)/;
mpp[fa[i]]=;
}
}
for(int i=;i<jishu;i++)
{
for(int j=i+;j<jishu;j++)
{
ans+=(b[i]*b[j]);
}
}
printf("%d\n",re-m+(n-sum)*maxn+ans);
return ;
}

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

  1. 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 ...

  2. 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/ ...

  3. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 暴力并查集

    D. Dividing Kingdom II 题目连接: http://www.codeforces.com/contest/687/problem/D Description Long time a ...

  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 #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 ...

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

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

  9. Codeforces Round #368 (Div. 2) B. Bakery (模拟)

    Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...

随机推荐

  1. POJ-2018(二分)

    //意是在一个数组里,寻找一段连续和,使其平均和最大,但是长度不能小于F, //首先可以看出是满足单调性的,但是怎么二分呢, //我们先枚举一个可能的数. //然后数组里的值全部减去这个值(结果会有正 ...

  2. winform圆角窗体实现

    winform圆角窗体实现 1.窗体的FormBorderStyle设置成None,不要控制边框 2.TransparencyKey和BackColor颜色设置成相同的,这样,窗体就透明了 3.以此为 ...

  3. 完美的【去重留一】SQL

    DELETE consum_record FROM consum_record, ( SELECT min(id) id, user_id, monetary, consume_time FROM c ...

  4. 构建树形结构数据(全部构建,查找构建)C#版

    摘要: 最近在做任务管理,任务可以无限派生子任务且没有数量限制,前端采用Easyui的Treegrid树形展示控件. 一.遇到的问题 获取全部任务拼接树形速度过慢(数据量大约在900条左右)且查询速度 ...

  5. I understand that you would like to know about the Amazon Giveaway

    Dear Seller, Greetings from Amazon Seller Support. From your mail, I understand that you would like ...

  6. wireshark解析https协议方法

    本文仅介绍通过协商密钥的方式实现https解析的方法 wireshark支持pem

  7. c++第三次作业

    GitHub地址 https://github.com/ronghuijun/3Elevators-scheduling 实现过程 一开始打算分成三个类来写的 因为想到电梯的功能不太一样 一个只能上1 ...

  8. lintcode-198-排列序号II

    198-排列序号II 给出一个可能包含重复数字的排列,求这些数字的所有排列按字典序排序后该排列在其中的编号.编号从1开始. 样例 给出排列[1, 4, 2, 2],其编号为3. 思路 和 lintco ...

  9. jdbc 1.0

    1. jdbc : java数据库连接技术 2.主要用到的类及接口 Class Driver ManagerDriver Connection Statement PreparedStatement ...

  10. p2 休眠模式

    如有错误,忘请指出. 才入手p2.p2有全局休眠模式,和钢体体眠模式.钢体能控制 body.allowSleep world.NO_SLEEPING 不允许休眠world.BODY_SLEEPING ...