F. Strings and Queries
time limit per test

2.5 s

memory limit per test

256 MB

input

standard input

output

standard output

You are given a set of n strings such that all characters in the strings are 'a', 'b', or 'c'.

Also, you are given q queries, such that each query consisting of two strings a and b. The answer of each query is the index of the string with the most number of palindrome substrings between strings a and b from the given set.

A substring of the string s is a sequence sl, sl + 1, ..., sr for some integers (l, r) such that (1 ≤ l ≤ r ≤ n), where n is the length of the string s.

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as "madam" or "racecar".

Input

The first line contains an integer T, where T is the number of test cases.

The first line of each test case contains two integers n and q (1 ≤ n ≤ 104) (1 ≤ q ≤ 105), where n is the number of strings, and q is the number of queries.

Then n lines follow, each line contains a non-empty string with length no more than 30, giving the strings. All characters in the strings are 'a', 'b', or 'c'. It is guaranteed that all strings are unique. The given strings are numbered from 1 to n.

Then q lines follow, each line contains two strings a and b, giving the queries. It is guaranteed that strings a and b exist in the given set of strings.

Output

For each query, print a single line containing the index of the string with the most number of palindrome substrings between the given strings a and b. If there are more than one answer, print the lowest index.

Example
Input

Copy
1
5 5
aaaaa
abaabc
cbbaca
abccba
abca
aaaaa abca
cbbaca abccba
abaabc abccba
abccba abca
abaabc abccba
Output

Copy
1
4
2
4
2
Note

As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.

这个是是一个RMQ的题目,

我分析一下我的思路,首先从学姐那里知道这个是一个RMQ的题目,然后就打开了这个模板(不太记得怎么写了),然后开始思考,这个是字符串,首先要把字符变成数字来处理,这个就需要一个数组或者map来储存它的位置,然后还要用一个map来储存每一个位置的子字符串数量,最后就是dp来储存编号,然后方便以后的储存。

之后的m次查询,每次找到它的位置在那里,然后用RMQ进行查询。

RMQ具体就是比较dp编号位置的字符回文子串的数量。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <map>
#define inf 0x3f3f3f3f
#define debug(n) printf("%d\n",n)
using namespace std;
typedef long long ll;
const int maxn = 1e4 + 100;
ll dp[maxn][30];
map < ll, ll > mp;
map<ll, ll>mm;
ll n, m; ll tras(char* s)
{
ll ans = 0;
for (int i = 0; i <s[i]; i++)//这里还是不太明白为什么这么写,有哪位大佬知道,可以告诉我一声。
{
ans = ans * 27 + s[i] - 'a' + 1;
}
return ans;
} ll getnum(char*s)
{
int len = strlen(s);
ll ans = 0;
for (int i = 0; i < len; i++)
{
for (int j = 0; i + j < len && i - j >= 0 && s[i - j] == s[i + j]; j++) ans++;
for (int j = 0; i + j < len && i - 1 - j >= 0 && s[i - 1 - j] == s[i + j]; j++) ans++;
}
return ans;
}
int max_(ll a, ll b)
{
if (mm[a] == mm[b]) return a < b ? a : b;
return mm[a] > mm[b] ? a : b;
} void RMQ()
{
for (int j = 1; (1 << j) <= n; j++)
{
for (int i = 1; i + (1 << j) - 1 <= n; i++)
{
dp[i][j] = max_(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
}
}
} int main()
{
int t;
cin >> t;
while (t--)
{
cin >> n >> m;
char s[100], s1[100];
mm.clear();
mp.clear();
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++)
{
scanf("%s", s);
mp[tras(s)] = i;
mm[i] = getnum(s);
dp[i][0] = i;
}
RMQ();
for (int i = 1; i <= m; i++)
{
scanf("%s%s", s, s1);
ll l = mp[tras(s)];
ll r = mp[tras(s1)];
if (l > r) swap(l, r);
ll k = (log(r - l + 1.0) / (log(2.0)));
ll num = max_(dp[l][k], dp[r - (1 << k) + 1][k]);
printf("%lld\n", num);
}
}
return 0;
}

  

RMQ 字符串 F. Strings and Queries的更多相关文章

  1. Go中的字符串使用----strings和strconv

    Go中的字符串操作 字符串是工作中最常用的,值得我们专门的练习一下.在Go中使用strings包来操作字符串,这也是内置的包哈,不像Java中要么手写,要么引入common-lang 或者 别的第三方 ...

  2. guava字符串工具 Strings 校验补全 转换null和""

    public class StringsTest { public static void main(String args[]){ //1.补右全(Strings.padEnd方法) String ...

  3. C# 6.0 内插字符串 (Interpolated Strings )

    讲Interpolated Strings之前,让我们先看EF Core 2.0 的一个新的特性:String interpolation in FromSql and ExecuteSqlComma ...

  4. [Swift]LeetCode854. 相似度为 K 的字符串 | K-Similar Strings

    Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two ...

  5. [Swift]LeetCode205. 同构字符串 | Isomorphic Strings

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  6. [Swift]LeetCode859. 亲密字符串 | Buddy Strings

    Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...

  7. PythonStudy——三种字符串 Three strings

    # 普通字符串:u'以字符作为输出单位'print(u'abc') # 用于显示 # 二进制字符串:b'' 二进制字符串以字节作为输出单位print(b'abc') # 用于传输 # 原义字符串:r' ...

  8. LeetCode 205:同构字符串 Isomorphic Strings

    题目: 给定两个字符串 s 和 *t*,判断它们是否是同构的. 如果 s 中的字符可以被替换得到 *t* ,那么这两个字符串是同构的. 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字 ...

  9. LeetCode 205. 同构字符串(Isomorphic Strings)

    205. 同构字符串 205. Isomorphic Strings

随机推荐

  1. [android] 保存联系人到系统通讯录

    对应着读联系人,把数据写进去,市场上的社交类应用经常会有这样的功能 向raw_contacts表中添加一个id 向data表里面添加对应的数据 获取ContentResolver对象,通过getCon ...

  2. SSM+Netty项目结合思路

    最近正忙于搬家,面试,整理团队开发计划等工作,所以没有什么时间登陆个人公众号,今天上线看到有粉丝想了解下Netty结合通用SSM框架的案例,由于公众号时间限制,我不能和此粉丝单独沟通,再此写一篇手记分 ...

  3. Eclipse中提示svn: is already locked的解决办法

    eclipse的svn提交不了,报错.提示 svn: is already locked   解决办法:右键项目-------Team------Refresh/Cleanup

  4. Java 导出 Excel 列号数字与字母互相转换工具

    package test; /** * Deal with Excel column indexToStr and strToIndex * @author * @version 2015-7-8 * ...

  5. HDU3440 House Man

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  6. 2019-01-13 [日常]mov文件转换为gif

    因为需要将之前mac下用QuickTime录屏生成的文件(mov格式)转换成gif文件, 便于传到某些博客平台, 于是找到了这个转换工具, 已将原代码的命名中文化并简化. Ruby和视频转换都是新手, ...

  7. Android SDK manager里面什么是必须下载的?

    最近公司来了位新同事,由于需要配置Android环境,但是在配置的时候却发现sdk很大,很占用空间,想复制给同事也觉得不方便,于是根据下面的图删除了一些不必要的api. 根据官方文档的描述SDK To ...

  8. Spring集成ElasticSearch搜索引擎

    目录 前期安装 Maven支持库安装 添加log4j的配置文件 创建Client客户端 实现增删改查以及符合查询 实现查询数据 实现添加数据 实现删除数据 实现修改数据 实现复合查询数据 Elasti ...

  9. python--文件流读写

    在讲述fileinput模块之前,首先说一下python内置的文件API—open()函数以及与其相关的函数. 我这里主要讲讲其中四个比较重要和常用的方法,更多的方法,可以参考:菜鸟教程http:// ...

  10. C# 代码中调用 Javascript 代码段以提高应用程序的配置灵活性(使用 Javascript .NET 与 Jint)

    一般来说,我们需要在开发应用软件的配置文件中,添加一些参数,用于后续用户根据实际情况,自行调整. 配置参数,可以放在配置文件中.环境变量中.或数据库表中(如果使用了数据库的话).通常,配置数据,以 k ...