UVALive - 5052 Genome Evolution 贪心
2015-03-01 15:40
330 查看
题目大意: 给出两个序列,统计一下有多少的二元组(a,b)满足以下条件:a是序列A的连续子序列,b是序列B的连续子序列,且a,b包含的数字完全相同
解题思路:记录序列B每个数字所在的位置,用MIN记录序列a在序列B中的最左端的位置,MAX记录序列a在序列B中最右端的位置,num记录这个子序列有多少个数字,如果满足MAX - MIN + 1 == num就表示满足
解题思路:记录序列B每个数字所在的位置,用MIN记录序列a在序列B中的最左端的位置,MAX记录序列a在序列B中最右端的位置,num记录这个子序列有多少个数字,如果满足MAX - MIN + 1 == num就表示满足
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; #define maxn 3010 int num1[maxn], num2[maxn]; int pos[maxn]; int n; int main() { while(scanf("%d",&n) == 1 && n) { for(int i = 1; i <= n; i++) scanf("%d",&num1[i]); for(int i = 1; i <= n; i++) { scanf("%d",&num2[i]); pos[num2[i]] = i; } long long ans = 0; for(int i = 1; i <= n; i++) { int MAX = pos[num1[i]], MIN = pos[num1[i]]; int num = 1; for(int j = i + 1; j <= n; j++) { if(pos[num1[j]] > MAX) MAX = pos[num1[j]]; if(pos[num1[j]] < MIN) MIN = pos[num1[j]]; num++; if(MAX - MIN + 1 == num) ans++; } } printf("%lld\n",ans); } return 0; }
相关文章推荐
- UVALive 7146 Defeat the Enemy(贪心+STL)(2014 Asia Shanghai Regional Contest)
- UVALive 3959 Rectangular Polygons (排序贪心)
- UVAlive 2911 Maximum(贪心)
- uvalive 4254 Processor处理器 (二分模拟+贪心)
- UVALive - 7146 Defeat the Enemy [贪心+multiset]
- UVALive 3530 Martian Mining(贪心,dp)
- uva 10716 Evil Straw Warts Live(贪心回文串)
- UVALive - 3507 Keep the Customer Satisfied 贪心
- UVALive 7147 World Cup(贪心+分类讨论)
- UVaLive 6609 Meeting Room Arrangement (贪心,区间不相交)
- uvalive 3971 - Assemble(二分搜索 + 贪心)
- UVALive 2326 Moving Tables 贪心
- UVALive 3971 Assemble 电脑配件 二分+贪心
- UVALive - 4987 Evacuation Plan DP+贪心
- UVA-10716 Evil Straw Warts Live(贪心+模拟)
- [贪心]UVA10716 - Evil Straw Warts Live
- UVALive 4225 Prime Bases 贪心
- UVALive 3177 - 长城守卫(二分+贪心)
- I - Defeat the Enemy UVALive - 7146 二分 + 贪心
- UVALive 4725 Airport 贪心 二分 模拟