Codeforces 798 B. Mike and strings-String的find()函数
好久好久好久之前的一个题,今天翻cf,发现这个题没过,补一下。
2 seconds
256 megabytes
standard input
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?
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.
Print the minimal number of moves Mike needs in order to make all the strings equal or print - 1 if there is no solution.
4
xzzwo
zwoxz
zzwox
xzzwo
5
2
molzv
lzvmo
2
3
kc
kc
kc
0
3
aa
aa
ab
-1
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函数,看其他人写的吧。
备忘一下。
Codeforces 798 B. Mike and strings-String的find()函数的更多相关文章
- 【codeforces 798B】Mike and strings
[题目链接]:http://codeforces.com/contest/798/problem/B [题意] 给你n个字符串; 每次操作,你可以把字符串的每个元素整体左移(最左边那个字符跑到最后面去 ...
- codeforces 798 D. Mike and distribution(贪心+思维)
题目链接:http://codeforces.com/contest/798/problem/D 题意:给出两串长度为n的数组a,b,然后要求长度小于等于n/2+1的p数组是的以p为下表a1-ap的和 ...
- codeforces 798 C. Mike and gcd problem(贪心+思维+数论)
题目链接:http://codeforces.com/contest/798/problem/C 题意:给出一串数字,问如果这串数字的gcd大于1,如果不是那么有这样的操作,删除ai, ai + 1 ...
- codeforces 798 D. Mike and distribution
D. Mike and distribution time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 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 ...
- CF410div2 B. Mike and strings
/* CF410div2 B. Mike and strings http://codeforces.com/contest/798/problem/B 字符串 暴力 题意:给你n个串,每次操作可以将 ...
- Mike and strings 798B
B. Mike and strings time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- 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 ...
随机推荐
- MySQL Group Replication数据安全性保障
本文来自数据库内核专栏 在之前的文章中,介绍了MGR对数据可靠性.可用性和一致性的实现方案.简单来说,MGR通过基于paxos协议的多副本来实现数据的可靠性,通过多副本上的majority机制来实现可 ...
- 《Cracking the Coding Interview》——第12章:测试——题目1
2014-04-24 23:10 题目:找出下面代码里的错误. 解法:请看下面. 代码: // 12.1 What's wrong with the following code segment? # ...
- 【UVA10655】 Contemplation! Algebra
题目 给定 \(p = a + b\) 和 \(q = ab\) 和 \(n\),求 \(a ^ n + b ^ n\). $0\le n\lt 2^{63} $ 分析 大水题. 先考虑 \(n\) ...
- paramiko类Fabric主机管理
环境:Linux python3.5 要求:类 Fabric 主机管理程序开发:1. 运行程序列出主机组或者主机列表2. 选择指定主机或主机组3. 选择让主机或者主机组执行命令或者向其传输文件(上传/ ...
- shell之进程
ps System V 风格 - -elF -ef -eF BSD a所有跟终端有关的进程 ...
- 软考——(1)J2SE
我们先从Java说起,简单的说,Java是一种面向对象的程序设计语言,可跨平台使用. 与之前学习的程序设计语言相比,最值得一提的就是Java的两种核心机制:Java虚拟机和垃圾回收机制. 1)虚拟机 ...
- 七、vue计算属性
细节流程图 初始化 计算属性的初始化是发生在 Vue 实例初始化阶段的 initState 函数中,执行了 if (opts.computed) initComputed(vm, opts.compu ...
- EXTJS4.0 显示图片 动态图片
在网上找了好久没有好的解决方法 都是用 'box' 什么的组件 改写的 autoEl 好麻烦,修改 好的属性都不能用.弄了好久没弄成 最后: 用panel 显示 html 文本 追加 ‘'<i ...
- hihoCoder #1902 字符替换
解法 这题比赛时过的人很多,我却没思路,糊里糊涂写了个强联通分量,得了 80 分. 这题思路是这样的. 一个替换操作可以看做一个有向边,所以题目实际上给出了一个有向图 $G$,一个节点代表一个字母. ...
- 了解Spark源码的概况
本文旨在帮助那些想要对Spark有更深入了解的工程师们,了解Spark源码的概况,搭建Spark源码阅读环境,编译.调试Spark源码,为将来更深入地学习打下基础. 一.项目结构 在大型项目中,往往涉 ...