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. Mybatis-Plus的填坑之路 - Lynwood/wunian7yulian

    目录 Mybatis-Plus 我来填坑~ 目录 一.简单介绍 官方说明 : 成绩: 最新版本: 开发层面MyBatis-Plus特色 Mybatis-Plus中的Plus 二.MP的特性 三.MP框 ...

  2. centos 系统初始化

    centos 系统初始化 #!/bin/bash # author cfwl create date of 2012-10-21 # blog http://cfwlxf.blog.51cto.com ...

  3. [shell] sed学习

    Q:匹配内容有1没有a的行 echo -e "1a\n2b\n1b\n2a" | sed -n '/1/{/a/d;p}' echo -e "1a\n2b\n1b\n2a ...

  4. ORA-01747

    java.sql.SQLException: ORA-01747: user.table.column, table.column 或列说明 语法中多了逗号 或者字段使用关键字

  5. c# 修改exe.config文件并且及时更新

    1.config文件地址:AppDomain.CurrentDomain.SetupInformation.ConfigurationFile 注意:如果是在调试程序中运行,此地址指代的是vhost. ...

  6. Hibernate(五)

    注解高级(原文再续书接上一回) 7.继承映射 第一种:InheritanceType.JOINED 查询时会出现很多join语句. package com.rong.entity.joined; im ...

  7. LR监控apache服务器

    开启mod_status模块功能,在LR的controller中找到apache资源图双击并右键添加度量,如下图:      添加apache服务器IP地址.选择系统平台.添加需要监控的计数器即可进行 ...

  8. bzoj4639 博士的选取器

    题意 给出一个长度为n的正整数序列,要求把它划分成若干个连续的区间,使得每个区间的数字之和都不超过给定的lim.最后的代价等于每个区间的最大值之和.求最小代价.n<=300000 分析 定义f[ ...

  9. AtCoder Regular Contest 074 瞎打记

    (很长时间没更新了>_<) 由于机房的网总是奥妙重重,开考30多分钟之后我才登进去... 然后发现T1是个简单枚举,1A.T2是个简单优先队列,1A.T3似乎需要一点推导,先看了T4发现是 ...

  10. Java实现Websocket

    Websocket介绍 在一个 WebSocket应用中, 服务器发布一个 WebSocket端点, 客户端使用这个端点的URI来连接服务器.建立连接之后,websocket协议是对称的;客户端和服务 ...