Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....".

Now we have another string p. Your job is to find out how many unique non-empty substrings of p are present in s. In particular, your input is the string p and you need to output the number of different non-empty substrings of p in the string s.

Note: p consists of only lowercase English letters.

Example 1:

Input: "a"
Output: 1 Explanation: Only the substring "a" of string "a" is in the string s.

Example 2:

Input: "cac"
Output: 2
Explanation: There are two substrings "a", "c" of string "cac" in the string s.

Example 3:

Input: "zab"
Output: 6
Explanation: There are six substrings "z", "a", "b", "za", "ab", "zab" of string "zab" in the string s.

  

  This is the third problem in weekly contest 11. It's true that there are many brilliant pipo. Hope next time is me.

  Causing there are too many common substrs at the end of any characters among 'a' - 'z'. How can I count them without repeating.

  A simply solution is calculating every sub-string in p and finding out if it is a common sub-string with s. But its too expensive in time which is O(n*n*n).

  The brilliant ideal is using one loop to travel p and using a  variable "pos" to record the mis-match position, thus we can get the maximum length of common string ending at every character('a' - 'z')。Finally,we count the number of common sub-string by the length (num = length). For example, "abc"'s max common substring len is 3. Common substrings are "abc" "bc" "c" .

  Here comes the code:

class Solution {
public:
int findSubstringInWraproundString(string p) {
int lenp = p.length(), cnt = , pos = ;
if(lenp <= ) return ;
vector<int>length(, ); /*length['a']:用来记录以'a'作为结尾的公共子串的最大长度 (因为结尾固定,按照相同的规律发展,长度越长那么细分出来的公共子串数目 = length 越多) */
p += '#';
lenp = p.length();
for(int i = ; i < lenp; i ++){
if((p[i] - 'a') % != (p[i - ] - 'a' + ) % ){
for(int j = pos; j < i; j ++)
length[p[j]] = max(length[p[j]], j - pos + );
pos = i;
}
}
for(int c = 'a'; c <= 'z'; c ++){
cnt += length[c];
}
return cnt;
}
};

  Do not waste any second in your life. Be strong , be confident.

最新文章

  1. HDU 2795 Billboard(区间求最大值的位置update的操作在query里做了)
  2. 在Flex4中嵌入字体
  3. DWZ中Tree树形菜单的treeCheck如何获取返回值解决方案
  4. 嵌入资源的方式让Winform使用系统没有的字体,无需安装字体
  5. MMM和MHA的对比和应用(PPT分享)
  6. 【转】 [C/OC的那点事儿]NSMutableArray排序的三种实现(依赖学生成绩管理系统).
  7. 实现3D摄像机缓冲系统的一些思考
  8. 动态设置uitableview高度,参考
  9. BZOJ3156: 防御准备
  10. Hibernate 使用说明
  11. C++Primer第5版学习笔记(四)
  12. 3D转换
  13. javascript第二课javascript规范
  14. 关于bootstrap弹出二级对话框的使用
  15. iTOP-4418开发板和6818开发板-第五路串口介绍
  16. EasyUI datagrid columns 中 field 区分大小写
  17. sql server 中DateName()函数及DatePart()函数
  18. 吴裕雄 python oracle子查询的用法(3)
  19. 在oracle配置mysql数据库的dblink
  20. 15. DML, DDL, LOGON 触发器

热门文章

  1. (一)U-Boot启动过程--详细版的完全分析
  2. 洛谷 2484 [SDOI2011]打地鼠
  3. 主流图数据库Neo4J、ArangoDB、OrientDB综合对比:架构分析
  4. 【转】建立一个更高级别的查询 API:正确使用Django ORM 的方式
  5. 类似于SVN的文档内容差异对比工具winmerge
  6. golang 的GOPATH设置的问题
  7. JavaScript设计模式 Item9 --适配器模式Adapter
  8. cocos2d的armature绑定到其它armature骨骼上的bug
  9. audio_device模块分析
  10. shell curl 实现rest 并发测试