好久好久好久之前的一个题,今天翻cf,发现这个题没过,补一下。

B. Mike and strings

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard 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?

Input

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

Output

Print the minimal number of moves Mike needs in order to make all the strings equal or print  - 1 if there is no solution.

Examples
input
4
xzzwo
zwoxz
zzwox
xzzwo
output
5
input
2
molzv
lzvmo
output
2
input
3
kc
kc
kc
output
0
input
3
aa
aa
ab
output
-1
Note

In the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".

题意:给定的n个串,最少能移动几次使得这n个字符串相等。每次只能将字符串首位的移动到末尾。

想法:以某个串为标准, 改变其他的字符串,找出变化次数最小的。

这个题用string中的find()函数,简直不要太炫酷。

代码:

 #include<bits/stdc++.h>
using namespace std;
const int N=+;
string a[N]; int main(){
int n,i,j;
while(~scanf("%d",&n)){
for(i=;i<n;i++)
cin>>a[i];
int ans=0x3f3f3f,flag=;
for(i=;i<n;i++){
int sum=;
for(j=;j<n;j++){
string tmp=a[j]+a[j];
if(tmp.find(a[i])==string::npos)flag=;
else sum+=tmp.find(a[i]);
}
ans=min(ans,sum);
}
printf("%d\n",flag?-:ans);
}
return ;
}

string中的find()函数,查找字符串A是否包含子串B,不是用strA.find(strB) > 0而是strA.find(strB) != string:npos

返回的值是B在A中第一次出现的下标位置,如果没有查询到,则返回string::npos,如果返回npos就输出-1。

关于string中的find函数,看其他人写的吧。

传送门:1.哇哇哇2.呜呜呜3.汪汪汪4.喂喂喂

备忘一下。

Codeforces 798 B. Mike and strings-String的find()函数的更多相关文章

  1. 【codeforces 798B】Mike and strings

    [题目链接]:http://codeforces.com/contest/798/problem/B [题意] 给你n个字符串; 每次操作,你可以把字符串的每个元素整体左移(最左边那个字符跑到最后面去 ...

  2. codeforces 798 D. Mike and distribution(贪心+思维)

    题目链接:http://codeforces.com/contest/798/problem/D 题意:给出两串长度为n的数组a,b,然后要求长度小于等于n/2+1的p数组是的以p为下表a1-ap的和 ...

  3. codeforces 798 C. Mike and gcd problem(贪心+思维+数论)

    题目链接:http://codeforces.com/contest/798/problem/C 题意:给出一串数字,问如果这串数字的gcd大于1,如果不是那么有这样的操作,删除ai, ai + 1 ...

  4. codeforces 798 D. Mike and distribution

    D. Mike and distribution time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  5. Codeforces Round #410 (Div. 2) B. Mike and strings

    B. Mike and strings time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. CF410div2 B. Mike and strings

    /* CF410div2 B. Mike and strings http://codeforces.com/contest/798/problem/B 字符串 暴力 题意:给你n个串,每次操作可以将 ...

  7. Mike and strings 798B

    B. Mike and strings time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  9. Codeforces 196 D. The Next Good String

    D. The Next Good String time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

随机推荐

  1. vue理解$nextTick

    首先要明确: Vue 实现响应式并不是数据发生变化之后 DOM 立即变化,而是按一定的策略进行 DOM 的更新. $nextTick 是在下次 DOM 更新循环结束之后执行延迟回调,在修改数据之后使用 ...

  2. MD5--3D模型

    在学习Away3D的过程中,接触到MD5模型和MD5动画这样两个词.之前对MD5的认识就是一种加密技术,怎么它又和动画扯上关系了呢. 一阵谷歌之后,终于在这个地方发现了关于3D方面的MD5介绍了:ht ...

  3. CSS系列(7)CSS类选择器Class详解

    这一篇文章,以笔记形式写. 1,  CSS 类选择器详解 http://www.w3school.com.cn/css/css_selector_class.asp 知识点: (1)    使用类选择 ...

  4. 一个初学者的辛酸路程-基于Django写BBS项目

    前言 基于Django的学习 详情 登录界面 找个模板 http://v3.bootcss.com/examples/signin/ 右键,检查源码     函数 def login(request) ...

  5. c# asp.net 中使用token验证

    基于token的鉴权机制类似于http协议也是无状态的,它不需要在服务端去保留用户的认证信息或者会话信息.这就意味着基于token认证机制的应用不需要去考虑用户在哪一台服务器登录了,这就为应用的扩展提 ...

  6. 孤荷凌寒自学python第十一天初识Python的字典类

    孤荷凌寒自学python第十一天初识Python的字典类 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) Python的字典其实是一张二维对照表 下面举例说明: 键名Key 姓名 性别 身高 ...

  7. asp+access win2008php+mysql /dedecms 配置总结

    1.  IIS 应用池  高级设置  启用32位应用程序:True 2. c盘window/Temp user 应该有管理权限           如果不行 creator owner 给予修改权限 ...

  8. input file request.files[] 为空

    需要在 form 里设置 一句话 :  $('form').attr("enctype", "multipart/form-data"); <form e ...

  9. 【bzoj5015】[Snoi2017]礼物 矩阵乘法

    题目描述 热情好客的请森林中的朋友们吃饭,他的朋友被编号为 1-N,每个到来的朋友都会带给他一些礼物:.其中,第一个朋友会带给他 1 个,之后,每一个朋友到来以后,都会带给他之前所有人带来的礼物个数再 ...

  10. [AtCoder ARC093F]Dark Horse

    题目大意:有$2^n$个人,每相邻的两个人比赛一次.令两个人的编号为$a,b(a\leqslant b)$,若$a\neq 1$,则$a$的人获胜:否则若$b\in S$则$b$获胜,不然$1$获胜. ...