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

大体意思就是给定一个字符串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的更多相关文章

  1. evernote出现“Sync failed due to unexpected problem at server side”的问题

    继上次的"Invalid username and/or password"问题之后,evernote又出现了“Sync failed due to unexpected prob ...

  2. kettle报错 ../deploy does not exist, please create it.

    具体错误如下: Xlib: extension "RANDR" missing on display "localhost:10.0". ::, INFO [K ...

  3. SLF4J源码解析-LoggerFactory(一)

    slf4j的含义为Simple logging facade for Java,其为简单的为java实现的日志打印工具,本文则对其源码进行简单的分析 JAVA调用SLF4J public class ...

  4. 日志那点事儿——slf4j源码剖析

    前言: 说到日志,大多人都没空去研究,顶多知道用logger.info或者warn打打消息.那么commons-logging,slf4j,logback,log4j,logging又是什么关系呢?其 ...

  5. Codeforces Round #481 (Div. 3)题解

    成功掉到灰,真的心太累了,orz!!!!,不是很懂那些国外大佬为什么每次都是20多分钟AK的,QAQ A. Remove Duplicates time limit per test 1 second ...

  6. kettle_Spoon 修改共享DB连接带汉字引发的错误

    win10下: kettle_Spoon 修改共享DB连接带汉字引发的错误: Unexpected problem reading shared objects from XML file : nul ...

  7. kettle_删除“共享输出表”引发的错误

    原创作品.出自 "深蓝的blog" 博客.欢迎转载,转载时请务必注明出处,否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong ...

  8. coedforces #481Div(3)(ABCDEFG)

    A. Remove Duplicates Petya has an array aconsisting of nintegers. He wants to remove duplicate (equa ...

  9. Adaptive device-initiated polling

    A method includes periodically sending a polling call to an enterprise system outside the firewall a ...

随机推荐

  1. 8.2.1.10 Nested-Loop Join Algorithms 嵌套循环 关联算法:

    8.2.1.10 Nested-Loop Join Algorithms 嵌套循环 关联算法: MySQL 执行关联在表之间使用一个嵌套循环算法或者变种 Nested-Loop Join Algori ...

  2. 【HDOJ】2577 How to Type

    DP. /* 2577 */ #include <cstdio> #include <cstring> #include <cstdlib> #define MAX ...

  3. 易pc,1G容量以上的u盘,另外还要一台装了windows系统的电脑。

    一.硬件:易pc,1G容量以上的u盘,另外还要一台装了windows系统的电脑. 二.软件: 1.windows xp的安装包.考虑到易PC的硬盘容量,还是用精简版的吧,比如“Deepin XP Li ...

  4. AOJ 0118 深度优先搜索

    日文题... 题意:一个面积为H*W的果园,种了苹果,梨和蜜柑.相邻(上下左右)的果树属于同一个区域,问果园共有多少个区域. 分析:迷宫问题.对于每一个格子,可以用深度优先搜索把相同果树的格子遍历并标 ...

  5. MD中bitmap源代码分析--状态机实例

    1. page_attrs的状态转换关系 之前说过,bitmap的优化核心是:bitmap设置后批量写入:bitmap延时清除.写bit用bitmap_statrwrite() + bitmap_un ...

  6. java-下载excel

    在java程序里面处理excel,我觉得比较方便的方式是先做出一个excel的模板(比如定义表头信息.表格名称等),然后根据这个模板往里面填充数据 我这里演示的是使用poi处理2007以上版本的exc ...

  7. 微信中web页面实现和公众号中查看图片一样的效果

    最近开发了一套资讯相关的web页面,嵌套在微信中,可支持点赞.评论等...在文章详情中,图片需要点击放大,随手势放大缩小,左右可滑动切换,总之类似于微信公众号效果. 开始想的方案是用轮播插件.或者在i ...

  8. SqlServer 笔记

    问题一:这标红色的符号 取掉 一直没有见过标红色的符号,尝试把这些符号粘贴出来到 notepad 发现它是乱码,尝试将它粘贴到sql查询分析器里,发现它显示空白.对于这种数据,一直想着找到这个acsi ...

  9. struts2讲义----二

    Struts的namespace 示例工程Struts2_0200_Namespace Struts.xml <struts> <constant name="struts ...

  10. C#自定义List类

    代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespac ...