集训队每周一赛2020-03-13(构造+找规律+字符串+并查集)

A 飞

Gym 102416C

传送门.

Let’s say we would like to give change of d dollars after a puchase of coffee. The only coins that we can use have values a, a+1, …, b. Is it possible to give out the change? We are interested in even harder quesion: for given d and a what is the smallest b such that the change can be given?

Input

The input is two numbers 1≤a≤d≤109.

Output

Print smallest b such that d is a sum of coins from range a,…,b.

Examples

Input
19 60
Output
20
Input
100 914
Output
102

题解

#include<stdio.h>
#include<math.h>
int main()
{
   
	int d,i,a,n,v;
	scanf("%d%d",&a,&d);
	n=d/a;
	v=d%a;
	i=ceil(1.0*v/n);
	printf("%d\n",a+i);		
	return 0;
}

B 流

传送门.

CodeForces 1316B

Vasya has a string s of length n. He decides to make the following modification to the string:
Pick an integer k, (1≤k≤n).
For i from 1 to n−k+1, reverse the substring s[i:i+k−1] of s. For example, if string s is qwer and k=2, below is the series of transformations the string goes through:
qwer (original string)
wqer (after reversing the first substring of length 2)
weqr (after reversing the second substring of length 2)
werq (after reversing the last substring of length 2)
Hence, the resulting string after modifying s with k=2 is werq.
Vasya wants to choose a k such that the string obtained after the above-mentioned modification is lexicographically smallest possible among all choices of k. Among all such k, he wants to choose the smallest one. Since he is busy attending Felicity 2020, he asks for your help.
A string a is lexicographically smaller than a string b if and only if one of the following holds:
a is a prefix of b, but a≠b;
in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.

Input

Each test contains multiple test cases.
The first line contains the number of test cases t (1≤t≤5000). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤5000) — the length of the string s.
The second line of each test case contains the string s of n lowercase latin letters.
It is guaranteed that the sum of n over all test cases does not exceed 5000.

Output

For each testcase output two lines:
In the first line output the lexicographically smallest string s′ achievable after the above-mentioned modification.
In the second line output the appropriate value of k (1≤k≤n) that you chose for performing the modification. If there are multiple values of k that give the lexicographically smallest string, output the smallest value of k among them.

Example

Input
6
4
abab
6
qwerty
5
aaaaa
6
alaska
9
lfpbavjsm
1
p
Output
abab
1
ertyqw
3
aaaaa
1
aksala
6
avjsmbpfl
5
p
1

Note

In the first testcase of the first sample, the string modification results for the sample abab are as follows :
for k=1 : abab
for k=2 : baba
for k=3 : abab
for k=4 : baba
The lexicographically smallest string achievable through modification is abab for k=1 and 3. Smallest value of k needed to achieve is hence 1.

题解

#include<iostream> 
#include<algorithm>
#include<string>
using namespace std;
string a,b,c,s,x;
int main()
{
   
    int t,n,i,ans;
    cin>>t;
    while(t--)
    {
   
        cin>>n;
        cin>>a;
        x=a;
        ans = 1;
        for(i=1; i<=n; i++) 
		{
   
            b=a.substr(0,i-1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
后缀自动机被广泛应用于OI竞赛中,特别是在字符串相关的问题中。它是一种高效的数据结构,能够有效地解决各种字符串匹配、模式匹配和计数等问题。 首先,后缀自动机可以用于解决最长公共子串和最长公共子序列等问题。对于给定的两个字符串,可以将其加入到后缀自动机中,并通过动态规划的方式求解最长公共子串或子序列的长度。 其次,后缀自动机还可以用于解决多次询问下的子串出现次数问题。通过构建全局后缀自动机,可以在O(n)的时间复杂度内预处理字符串,并在O(m)的时间复杂度内得出任意子串的出现次数,其中n为字符串长度,m为询问总数。 另外,后缀自动机还可以用于解决包含多模式匹配的问题。通过将模式串加入到后缀自动机中,并预处理自动机的fail指针,可以在O(n)的时间复杂度内到所有模式串在文本中的出现位置。这在处理大规模的文本匹配问题时非常有用。 此外,后缀自动机还可以进行字符串的字典序统计。通过在构建自动机时记录每个节点的信息,可以在O(n)的时间复杂度内得到字符串的字典序第k小/大的子串。 总之,后缀自动机在OI竞赛中有着广泛的应用,能够解决各种字符串相关的问题。通过巧妙地构建自动机,并充分利用其性质,可以实现高效的字符串算法,为解决复杂的字符串问题提供了有力的工具。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值