• 问题 K: Summer Trip


    题目描述
    Leo has started a job in a travel agency. His first task is to organize a summer trip to an exotic overseas city. During the summer season, events of various types take place in the city: sports matches, concerts, beach parties, and many others. At any given time, there is exactly one event taking place. Events of any particular type may take place more than once during the season. The itinerary of events that Leo offers to his clients cannot be chosen arbitrarily; the company
    requires them to form a so-called “good itinerary.” A good itinerary is a consecutive sequence of at least two events in the summer season, where the first and last events are of different types, and they are both unique among all event types during the sequence. For example, if the first event in a good itinerary is a beach party, none of the other events during the itinerary can also be a beach party. There are no other restrictions on the event types in the sequence of a good itinerary.
    Before he starts organizing the trip, Leo wants to know the total number of good itineraries that are possible given a calendar of events that will take place over the summer season.

    输入
    The input consists of one line with a string describing the sequence of event types in the summer season. All characters are lowercase English letters ( ’a’ – ’z’ ), with different letters represent different types of events. Character i of the string encodes the ith event of the summer. There are no blanks or spaces in the string.
    The length of the input string is at least 2 and at most 100 000 characters.

    输出
    Print the number of good itineraries that exist for the given summer season.

    样例输入
    复制样例数据
    abbcccddddeeeee
    样例输出
    10

    一个很水的题目, 但是我没想到直接暴力写, 反而想要简单的方法, 结果没找到,还没做出来, 直接枚举起点, 然后再一个枚举终点, 如果两者相同了, 那么肯定不能贡献一个答案了, 因为这个j = i + 1 , j ++ , 也就会出现起点出现在区间(i , j), 里面了, 然后j 再加加的话, 还是这个结果,有重复的出现了,所以就直接break了, 然后就是判断结尾有无出现区间里面, 可以直接做一下标记, 如果没有出现过, 那么就直接答案贡献一, 然后打上标记出现过了, 还有一个点就是用一个k来进行计数, 如果出现不同的字母26次了, 就可以结束了,因为第27次出现的,肯定会和之前的进行至少一次的重复, 就可以直接break了

    #include <iostream>
    #include <cstring>
    #pragma GCC optimize(3 , "Ofast" , "inline")
    using namespace std;
    int vis[22200] ;
    typedef long long ll ;
    int main()
    {
    	string s ;
    	cin >> s ;
    	int len = s.size() ;
    	ll ans = 0 ;
    	for(int i = 0 ;i < len ; i ++)
    	 {
    	    int k = 1 ;
    	    memset(vis , 0 , sizeof vis) ;
    	    for(int j = i + 1 ;j < len && k <= 26 ;j ++)
    	     {
    	     	if(s[j] == s[i]) break ;
    	     	if(vis[s[j]] == 0)
    	     	vis[s[j]] = 1 ,	ans ++ , k ++ ;
    		 }
    	 }
    	 printf("%lld
    " , ans) ;
    	return 0 ;
    } 
    
    每次做题提醒自己:题目到底有没有读懂,有没有分析彻底、算法够不够贪心、暴力够不够优雅。
  • 相关阅读:
    ThinkPHP-5.0.23新的RCE漏洞测试和POC
    利用DNS日志进行MySQL盲注
    Prometheus自动发现
    relabel_config
    Maven安装
    Redis安装
    Node.js安装
    MySQL-5.7安装
    设计模式【1】------>单例模式
    Java设计模式概念以及六大设计原则
  • 原文地址:https://www.cnblogs.com/spnooyseed/p/12870880.html
Copyright © 2020-2023  润新知