unexpected problem
一个比较有趣的字符串问题,问题描述如下

大体意思就是给定一个字符串s以及一个整数m,找出一个能满足以上三个条件的字符串t的个数对10e9 + 7 取余输出。
第二三条是关键,t.s = s.t 举个例子 s = abab, t的话可以是ab。那么 t.s = ab(t)abab(s),s.t = abab(s)ab(t)。
t的长度要小于m。
例子:

一种大体思路就是判断s是否为由循环子字符构成的字符串,如abababab为子字符串ab循环构成的字符串。 如果是循环串,则找出子串的长度(len),如果不是则取s的长度(len)。 则(m/len)%10e9 + 7 即为所求。
一种判断是否为循环串的方法:
1.对s的长度进行因子分解
2.依次对因子进行遍历,按照因子长度对s进行截取,如果各子串都相同则记录因子跳出遍历,所找出的字符串即为所需。
#include <iostream>
#include <cstring>
#include <vector>
#include <cmath>
#include <cstdio> using namespace std; int main(){
string s,preStr;
cin>>s;
int len = s.length();
vector<int> idx;
vector<string> sub;
//bool flag = true;
for(int i = ; i < len ; i++){
if(len%i==)
idx.push_back(i);
}
int sz = idx.size();
for(int i = ; i < sz ; i++){
int step = idx[i];
preStr = s.substr(,step);
bool flag = true;
for(int j = step; j < len/step ; j++){
if(s.substr(idx[i]*j, idx[i]) != preStr)
{
flag = false;
break;
}
}
if(flag == true)
break;
} cout<<preStr.length()<<endl; }
以上为找字符串串长度代码,可根据题目稍作修改即可。
leaderboard上面一个比较简洁的代码,基本思想相同。
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string s;
long m;
cin >> s >> m;
for (int i = ; i <= s.size(); i++) {
if (s.size() % i != ) continue;
int j;
for (j = i; j < s.size(); j++) {
if (s[j] != s[j%i]) break;
}
if (j == s.size()) {
cout << (m/i % (long)(1e9+)) << endl;
break;
}
}
return ;
}
unexpected problem的更多相关文章
- evernote出现“Sync failed due to unexpected problem at server side”的问题
继上次的"Invalid username and/or password"问题之后,evernote又出现了“Sync failed due to unexpected prob ...
- kettle报错 ../deploy does not exist, please create it.
具体错误如下: Xlib: extension "RANDR" missing on display "localhost:10.0". ::, INFO [K ...
- SLF4J源码解析-LoggerFactory(一)
slf4j的含义为Simple logging facade for Java,其为简单的为java实现的日志打印工具,本文则对其源码进行简单的分析 JAVA调用SLF4J public class ...
- 日志那点事儿——slf4j源码剖析
前言: 说到日志,大多人都没空去研究,顶多知道用logger.info或者warn打打消息.那么commons-logging,slf4j,logback,log4j,logging又是什么关系呢?其 ...
- Codeforces Round #481 (Div. 3)题解
成功掉到灰,真的心太累了,orz!!!!,不是很懂那些国外大佬为什么每次都是20多分钟AK的,QAQ A. Remove Duplicates time limit per test 1 second ...
- kettle_Spoon 修改共享DB连接带汉字引发的错误
win10下: kettle_Spoon 修改共享DB连接带汉字引发的错误: Unexpected problem reading shared objects from XML file : nul ...
- kettle_删除“共享输出表”引发的错误
原创作品.出自 "深蓝的blog" 博客.欢迎转载,转载时请务必注明出处,否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong ...
- coedforces #481Div(3)(ABCDEFG)
A. Remove Duplicates Petya has an array aconsisting of nintegers. He wants to remove duplicate (equa ...
- Adaptive device-initiated polling
A method includes periodically sending a polling call to an enterprise system outside the firewall a ...
随机推荐
- Cracking the coding interview--Q1.8
原文: Assume you have a method isSubstring which checks if one word is a substring of another. Given t ...
- 2个Web上传组件
http://www.uploadify.com/download/ http://gmupload.tanjun.com.cn/
- ChangeServiceConfig2 function
ChangeServiceConfig2 function Changes the optional configuration parameters of a service. Syntax C ...
- mySql控制流程的函数
1.select case value then result else value end; 在第一个方案的返回结果中, value=compare-value.而第二个方案的返回结果是第一种情况的 ...
- Linux sar使用
[root@ywcrmdb ~]# sar -d 1 10 Linux 2.6.32-220.el6.x86_64 (ywcrmdb) 2014年04月19日 _x86_64_ (4 CPU) 1 ...
- 2015第24周四Spring事务4
Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. Da ...
- Delphi default属性
Delphi default属性 Delphi控件指定属性的时候可以加上Default关键字,例如property Color default clBtnface.一直以为这个是构造类的时候的默认值, ...
- [置顶] 白话最小边覆盖总结--附加 hdu1151结题报告
刚开始看到这个题目的时候就觉得想法很明了,就是不知道如何去匹配... 去网上看了不少人的解题报告,但是对于刚接触“最小边覆盖”的我来说....还是很困难滴....于是自己又开始一如以往学习“最大独立集 ...
- awr报告基本操作
1.查看当前的AWR保存策略.设置:快照间隔.保存时间. SQL> col SNAP_INTERVAL format a20 SQL> col RETENTION format a2 ...
- 关于springMVC框架访问web-inf下的jsp文件
问题:springMVC框架访问web-inf下的jsp文件,具体如下: 使用springMVC,一般都会使用springMVC的视图解析器,大概会这样配置 <property name=&qu ...