首页 > 学院 > 开发设计 > 正文

1149_子串计算

2019-11-14 11:35:04
字体:
来源:转载
供稿:网友
// 1149_子串计算.cpp : 定义控制台应用程序的入口点。////题目1149:子串计算//时间限制:1 秒内存限制:32 兆特殊判题:否提交:1121解决:644//题目描述://给出一个01字符串(长度不超过100),求其每一个子串出现的次数。//输入://输入包含多行,每行一个字符串。//输出://对每个字符串,输出它所有出现次数在1次以上的子串和这个子串出现的次数,输出按字典序排序。//样例输入://10101//样例输出://0 2//01 2//1 3//10 2//101 2//来源://2010年北京大学计算机研究生机试真题#include "stdafx.h"#include "iostream"#include "map"#include "string.h"using namespace std;int main(){ char str[102]; while(cin>>str){ map <string,int> cnt; //map容器中的元素自动按键的字典序排序,其中值初始化为0 for(int i=0;i<strlen(str);i++) for(int j=i;j<strlen(str);j++){ string tmp(str+i,j-i+1); //string构造函数,从str+i位置开始的j-i+1个字符 cnt[tmp]++; //map类型最简单的添加数据的方法 } for(map<string,int>::iterator iter = cnt.begin();iter!=cnt.end();iter++) if (iter->second >1) cout<<iter->first<<" "<<iter->second<<endl; } return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表