九度oj题目1555:重复子串

题目1555:重复子串

时间限制:3 秒

内存限制:256 兆

特殊判题:

提交:738

解决:125

题目描述:

给定一个由小写字母组成的字符串,求它的所有连续子串中,出现过至少两次,且至少有一对出现的重复子串是不重合的连续子串个数。
如给定字符串aaaa,aa和a,符合条件,aaa不符合条件(出现重合),故答案为2。

输入:

输入包含多组测试用例,每组测试用例包含一个字符串,由小写字母组成,其长度不大于1000。

输出:

对于每组测试数据,输出一个整数,代表符合条件的子串个数。

样例输入:
aaaa
aaa
样例输出:
2
1
来源:
2014年王道论坛计算机考研机试全真模拟考试
学习链接:http://blog.csdn.net/u013491262/article/details/21406757
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cmath>
 5 #include <string>
 6 #include <set>
 7 using namespace std;
 8 int main(){
 9     set<string> get;
10     string s;
11     while(cin>>s){
12         get.clear();
13         int len,st;
14         int i,j;
15         for(i=0;i<s.length();i++){
16             for(j=1;i+j+j<=s.length();j++){
17                 string be=s.substr(i,j);
18                 string en=s.substr(i+j);
19                 if(en.find(be)!=string::npos){
20                     get.insert(be);
21                 }
22                 else{
23                     break;//同一个i情况下,如果当前的be在en找不到,则更长的be在对应的en中肯定找不到
24                 }
25             }
26         }
27         cout<<get.size()<<endl;
28     }
29     return 0;
30 }
原文地址:https://www.cnblogs.com/Deribs4/p/4631708.html