hihocoder#1152 : Lucky Substrings

字串处理操作,用到了stl的排序和去重

#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

int fib[]={1,2,3,5,8,13,21,34,55,89};
vector<string>sub;

int main()
{
    char line[210];
    while(scanf("%s",line)!=EOF)
    {
        sub.clear();
        int len = strlen(line);
        for(int i = 0; i < len; i++)
            for(int j = i; j < len; j++)
            {
                int low[30]={0},sum = 0,flag = 0;
                for(int k = i; k <= j; k++)
                    low[line[k]-‘a‘] ++;
                for(int k = 0; k < 26; k++)
                    if(low[k]) sum++;

                for(int k = 0; k < 10; k++)
                    if(sum == fib[k])
                    {
                        flag = 1;
                        break;
                    }
                if(flag)
                {
                    string tmp="";
                    for(int k = i; k <= j; k++)
                        tmp+=line[k];
                    sub.push_back(tmp);
                }
            }
        sort(sub.begin(),sub.end());
        vector<string>::iterator it_u = unique(sub.begin(),sub.end());
        sub.erase(it_u,sub.end());
        vector<string>::iterator it=sub.begin(),ed = sub.end();
        for(;it != ed; it++)
        {
            cout<<*it<<endl;
        }
    }
    return 0;
}
时间: 2024-08-29 21:08:34

hihocoder#1152 : Lucky Substrings的相关文章

hihocoder #1152 Lucky Substrings 【字符串处理问题】strsub()函数+set集合去重

#1152 : Lucky Substrings时间限制:10000ms单点时限:1000ms内存限制:256MB描述A string s is LUCKY if and only if the number of different characters in s is a fibonacci number. Given a string consisting of only lower case letters, output all its lucky non-empty substrin

微软2016校招笔试 第二场部分题目个人思路

A. Lucky Substrings 这道题并不难,由于字符串长度只有100,那么它的子串肯定不超过1w个,枚举出所有字串都是没有问题的,至于检验一个子串里面不同的字母数量是不是斐波那契数,我们只需要事先把斐波那契数列小于1w的项都生成出来,然后枚举一个子串之后,统计出不同字母的数量(边找边统计,如果当前字母之前出现过就不加,如果没出现过就记住并+1),去这个里面找就行了.斐波那契数列推不了几项就到1w了-- 最后要字典序从小到大排列,并且还要去重,那就用string然后直接sort+uniq

hihoCoder 1426 : What a Ridiculous Election(总统夶选)

hihoCoder #1426 : What a Ridiculous Election(总统夶选) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 In country Light Tower, a presidential election is going on. There are two candidates,  Mr. X1 and Mr. X2, and both of them are not like good per

[Leetcode] DP-- 467. Unique Substrings in Wraparound String

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

[hihoCoder#1381]Little Y&#39;s Tree

[hihoCoder#1381]Little Y's Tree 试题描述 小Y有一棵n个节点的树,每条边都有正的边权. 小J有q个询问,每次小J会删掉这个树中的k条边,这棵树被分成k+1个连通块.小J想知道每个连通块中最远点对距离的和. 这里的询问是互相独立的,即每次都是在小Y的原树上进行操作. 输入 第一行一个整数n,接下来n-1行每行三个整数u,v,w,其中第i行表示第i条边边权为wi,连接了ui,vi两点. 接下来一行一个整数q,表示有q组询问. 对于每组询问,第一行一个正整数k,接下来一

hihoCoder 1175:拓扑排序二

题目链接: http://hihocoder.com/problemset/problem/1175 题目难度:一星级(简单题) 今天闲来无事,决定刷一道水题.结果发现这道水题居然把我卡了将近一个钟头. 最后终于调通了.总结起来,原因只有一个:不够仔细. 思路不用细说了,就是拓扑排序的简单应用.然而,一些不起眼的细节才是让你掉坑里的真正原因. 猜猜哪儿可能出bug? // A simple problem, but you can't be too careful with it. #inclu

uva 10829 - L-Gap Substrings(后缀数组)

题目链接:uva 10829 - L-Gap Substrings 题目大意:给定一个字符串,问有多少字符串满足UVU的形式,要求U非空,V的长度为g. 解题思路:对字符串的正序和逆序构建后缀数组,然后枚举U的长度l,每次以长度l分区间,在l和l+d+g所在的两个区间上确定U的最大长度. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using nam

hihocoder [Offer收割]编程练习赛18 C 最美和弦(dp)

题目链接:http://hihocoder.com/problemset/problem/1532 题解:一道基础的dp,设dp[i][j][k][l]表示处理到第几个数,当前是哪个和弦错了几次初始x值是多少.这里还要再辅助一个val[k]表示处理到当前情况只错了k次的最小值是多少因为改变的不止是和弦还有初始值,可以看一下代码理解一下. #include <iostream> #include <cstring> #include <cstdio> #include &

11.6 项目: “I’m Feeling Lucky”百度 查找

1 #! python3 2 # lucky.py - Opens several Google search results. 3 import requests, sys, webbrowser,logging 4 from bs4 import BeautifulSoup 5 logging.disable(logging.CRITICAL) 6 logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelna