CF-798B
B. Mike and stringstime limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In one move he can choose a string si, erase the first character and append it to the end of the string. For example, if he has the string "coolmike", in one move he can transform it into the string "oolmikec".
Now Mike asks himself: what is minimal number of moves that he needs to do in order to make all the strings equal?
InputThe first line contains integer n (1 ≤ n ≤ 50) — the number of strings.
This is followed by n lines which contain a string each. The i-th line corresponding to string si. Lengths of strings are equal. Lengths of each string is positive and don't exceed 50.
OutputPrint the minimal number of moves Mike needs in order to make all the strings equal or print - 1 if there is no solution.
Examplesinput4
xzzwo
zwoxz
zzwox
xzzwooutput5input2
molzv
lzvmooutput2input3
kc
kc
kcoutput0input3
aa
aa
aboutput-1NoteIn the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".
题意:
将所有字符串变成相等,只允许将最左边的字符移到最右,问最少要移多少步。
若不能使所有相等,则输出-1、
分别以每一个字符串为模板,将其他的字符串移成和当前字符串相等的情况,再找出步数最少的方案。
附AC代码:
#include<bits/stdc++.h>
using namespace std; const int inf=<<; string s[]; int main(){
int n,i,j,k,m;
cin>>n;
for(i=;i<n;i++){
cin>>s[i];
}
int len=s[].size();
int ans=inf;
for(i=;i<n;i++){
int cnt=;
for(j=;j<n;j++){
for(m=;m<len;m++){
for(k=;k<len;k++){
if(s[i][k]!=s[j][(k+m)%len])
break;
}
if(k==len)//只有len个都相等才表明移动m个字符后两字符串相等
break;
}
if(m==len){//若m==len则表明不能匹配。
cnt=inf;
break;
}
cnt+=m;
}
ans=min(ans,cnt);
}
if(ans==inf)
cout<<-<<endl;
else
cout<<ans<<endl;
return ;
}
CF-798B的更多相关文章
- CF 798B 渣渣题
题目链接:http://codeforces.com/contest/798/problem/B 此题是我打河工大校赛前一晚熬夜打CF时硬肛过去的B题,今天补题时,偶然看到dalao的代码,ORZ,s ...
- ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'
凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- cf Round 613
A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...
- ARC下OC对象和CF对象之间的桥接(bridge)
在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...
- [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
- CF memsql Start[c]UP 2.0 A
CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...
- CF memsql Start[c]UP 2.0 B
CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...
- CF #376 (Div. 2) C. dfs
1.CF #376 (Div. 2) C. Socks dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...
- CF #375 (Div. 2) D. bfs
1.CF #375 (Div. 2) D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...
随机推荐
- wamp中apache2.4.9配置httpd.conf允许外部访问
安装最新的wamp后发现通过外部网络无法访问本机的apache.在网上查询了相关问题,所有的答案基本都是说在httpd.conf文件中加入语句Allow from all.但是这些对应的是ap ...
- initializer_list、初始化列表、列表初始化
什么是列表初始化 使用一个花括号来初始化变量,表现形式如下: std::vector<int>a{1,2,3,4,5}; 或者 std::vector<int>a = {1,2 ...
- jsp页面设置绝对路径
例子: 设置完之后就随便jsp页面放在哪个文件夹都可以加载到资源了 <%@ page language="java" contentType="text/html; ...
- TCP/IP协议组学习笔记
TCP/IP协议族学习笔记: 一.基础概念: (1)TCP(Transmission Control Protocol) 传输控制协议. (2)IP(Internet Protocol)网际协议.IP ...
- cygwin配置个人环境,android模拟器root映象和Babun
零.Windows命令行个人设置 @echo off :: Temporary system path at cmd startup ::set PATH=%PATH%;"C:\Progra ...
- [oracle]pl/sql --分页过程demo
这句sql能够用来查询一张表中的特定位置的记录 --查询的方法获取分页的语句 select *from (select t1.*,rownum rn from (select *from books) ...
- CentOS 7.2 , YUM 方式安装VSC
1.引入VSC站点key和repo地址 sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc sudo sh -c ' ...
- RFID 卡片防复制
RFID 卡片防复制 这里的卡指的 MIFARE Classic 系列 或 NTAG 系列的卡.其它的卡没接触过,不了解. 防伪有两层意义: 卡片防复制.比如小区卡被复制,使得出入小区的人员管理更困难 ...
- ubuntu手机识别
1.sudo gedit /etc/udev/rules.d/51-android.rules 2.将以下内容拷贝保存 SUBSYSTEM=="usb", ATTR{idVendo ...
- oracle指定访问某表或某视图
在oracle中,想创建一个账号,然后只能只读地访问指定的表,怎么搞? 一.为特定的表创建视图 创建视图的时候还可以加上过滤条件,连访问哪些数据都可以指定. create or replace vie ...