uva10340 All in All C语言

You have devised a new encryption technique which encodes a message by inserting between its characters
randomly generated strings in a clever way. Because of pending patent issues we will not discuss in
detail how the strings are generated and inserted into the original message. To validate your method,
however, it is necessary to write a program that checks if the message is really encoded in the final
string.
Given two strings s and t, you have to decide whether s is a subsequence of t, i.e. if you can remove
characters from t such that the concatenation of the remaining characters is s.
Input
The input contains several testcases. Each is specified by two strings s, t of alphanumeric ASCII
characters separated by whitespace. Input is terminated by EOF.
Output
For each test case output, if s is a subsequence of t.
Sample Input
sequence subsequence
person compression
VERDI vivaVittorioEmanueleReDiItalia
caseDoesMatter CaseDoesMatter
Sample Output
Yes
No
Yes
No

#include<stdio.h> 
#include<string.h>
int main()
{
	char a[1000],b[1000];
	while(scanf("%s%s",a,b)!=EOF)
	{
		int la=strlen(a);
		int lb=strlen(b);
		int i,k=0;
		for(i=0;i<lb;i++)
		{
			if(b[i]==a[k])
			{  
			   k++;
			}   
			if(k==la)
			   break;  
		}
		if(k==la)
		  printf("yes\n");
		else
		   printf("no\n");
	} 
}
### UVa 1225 C语言解决方案 UVa 1225 是一个经典的贪心算法问题,题目描述涉及合并果子的过程。为了有效地解决问题,在编写C语言程序时应遵循贪心策略来最小化总成本。 #### 使用优先队列优化合并过程 由于每次操作都需要选取两个最小的堆进行合并,因此可以利用优先队列(最小堆)数据结构来高效获取当前最小值。通过不断取出两个最小元素并将其合并后的结果重新加入到优先队列中直到只剩下一个元素为止[^1]。 ```c #include <stdio.h> #include <stdlib.h> typedef struct { int *data; int size; } MinHeap; void swap(int* a, int* b){ int temp = *a; *a = *b; *b = temp; } // 插入新节点 void insert(MinHeap* heap, int value) { if (heap->size >= 10000) return; // 防止溢出 int i = ++(heap->size); while ((i != 1) && (value < heap->data[i / 2])) { heap->data[i] = heap->data[i / 2]; i /= 2; } heap->data[i] = value; } int extractMin(MinHeap* heap) { int minItem = heap->data[1]; int lastItem = heap->data[(heap->size)--]; int parentIndex = 1; int childIndex; do { childIndex = parentIndex * 2; if((childIndex < heap->size) && (heap->data[childIndex + 1] < heap->data[childIndex])) childIndex++; if(childIndex <= heap->size && lastItem > heap->data[childIndex]) { heap->data[parentIndex] = heap->data[childIndex]; parentIndex = childIndex; } else break; }while(childIndex <= heap->size); heap->data[parentIndex] = lastItem; return minItem; } ``` 此代码片段实现了基本的最小堆功能,可用于处理输入的数据集,并按照上述提到的方法完成合并操作以求解最低代价。 #### 输入输出部分 对于具体的输入输出逻辑,则需根据实际测试案例的要求来进行调整: ```c int main() { int n; scanf("%d", &n); MinHeap h = {malloc(sizeof(int)*10000), 0}; for (int i=0;i<n;++i){ int w; scanf("%d",&w); insert(&h,w); } long long cost = 0LL; while(h.size>1){ int first_min = extractMin(&h); int second_min = extractMin(&h); int sum = first_min + second_min; cost += sum; insert(&h,sum); } printf("%lld\n",cost); free(h.data); return 0; } ``` 该段代码展示了完整的读取输入、构建初始状态下的最小堆以及执行合并计算最终花费的核心流程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值